Java Spring Data 不处理 Pageable 操作参数创建

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22135002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 13:49:55  来源:igfitidea点击:

Spring Data does not handle Pageable action argument creation

javaspringspring-mvcspring-data

提问by Rafa? Wrzeszcz

I have a simple controller action:

我有一个简单的控制器操作:

public class CategoriesController
{
    @RequestMapping(value = { "/", "" })
    public String list(
        Model model,
        @PageableDefault(size = CategoriesController.PAGE_LIMIT) Pageable pager
    )
    {
        // load page data
        Page<Category> page = this.categoryService.findAll(pager);

        /* action logic here */
    }
}

Here is my pom.xmlfragment:

这是我的pom.xml片段:

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>3.2.4.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>3.2.4.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>3.2.4.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-commons</artifactId>
        <version>1.6.4.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>1.5.0.RELEASE</version>
    </dependency>

After addtion this to my applicationContext.xml:

将此添加到我的之后applicationContext.xml

<bean class="org.springframework.data.web.config.SpringDataWebConfiguration"/>

I have following error:

我有以下错误:

org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.data.domain.Pageable]: Specified class is an interface

Spring Data itself works fine, JPA repositories are working. But until now I had hand-written pagination in controllers (calculating pages on my own, creating PageRequestobjects by hand). I wanted to make use of Spring Data web extras, but they don't work for me for some reason... registering obsolete org.springframework.data.web.PageableArgumentResolverby hand partially made it working, but not completely, but still, I don't think this should even be a solution.

Spring Data 本身工作正常,JPA 存储库工作正常。但是直到现在我在控制器中手写分页(自己计算页面,PageRequest手动创建对象)。我想使用 Spring Data web extras,但由于某种原因它们对我不起作用......org.springframework.data.web.PageableArgumentResolver手动注册过时部分使其工作,但不完全,但仍然,我认为这甚至不应该是一个办法。

After enabling debug logger on org.springframeworkI see that:

在启用调试记录器后,org.springframework我看到:

01:37:33.850 [localhost-startStop-1] DEBUG org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader - Registering bean definition for @Bean method org.springframework.data.web.config.SpringDataWebConfiguration.pageableResolver()

So it's registered - any idea why it's not working?

所以它已注册 - 知道为什么它不起作用吗?

采纳答案by M. Deinum

Your problem is that you are trying to mix XML configuration and Java Config based configuration. In this particular case that isn't going to work. The bean in the configuration class will be instantiated but that is it, they aren't registered to your <mvc:annotation-driven />configuration.

您的问题是您正在尝试混合 XML 配置和基于 Java Config 的配置。在这种特殊情况下,这是行不通的。配置类中的 bean 将被实例化,但就是这样,它们没有注册到您的<mvc:annotation-driven />配置中。

You will have to add the beans manually to the ConversionServiceand your RequestMappingHandlerMapping. Either that our switch, at least your DispatcherServletconfiguration to Java Config.

您必须手动添加豆类的ConversionService和你的RequestMappingHandlerMapping。要么是我们的switch,至少是你的DispatcherServlet配置到Java Config。

In XML you can configure additional argument-resolvers by using the <mvc:argument-resolvers />tag. (This mimics the configuration from the SpringDataWebConfiguration).

在 XML 中,您可以使用<mvc:argument-resolvers />标签配置额外的参数解析器。(这模仿了 中的配置SpringDataWebConfiguration)。

<mvc:annotation-driven>
    <mvc:argument-resolvers>
        <ref bean="sortResolver"/>
        <ref bean="pageableResolver" />
    </mvc:argument-resolvers>
</mvc:annotation-driven>

<bean id="sortResolver" class="org.springframework.data.web.SortHandlerMethodArgumentResolver" />
<bean id="pageableResolver" class="org.springframework.data.web.PageableHandlerMethodArgumentResolver">
    <constructor-arg ref="sortResolver" />
</bean>

However the SpringDataWebConfigurationdoes more then only these 2 resolvers it also registers a DomainClassConverter. If you also want to use that you need some additional configuration.

然而,SpringDataWebConfiguration除了这两个解析器之外,它还注册了一个DomainClassConverter. 如果您还想使用它,则需要一些额外的配置。

<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean" />

<bean class="org.springframework.data.repository.support.DomainClassConverter">
   <constructor-arg ref="conversionService" />
</bean>

<mvc:annotation-driven conversion-service="conversionService">
    <mvc:argument-resolvers>
        <ref bean="sortResolver"/>
        <ref bean="pageableResolver" />
    </mvc:argument-resolvers>
</mvc:annotation-driven>

<bean id="sortResolver" class="org.springframework.data.web.SortHandlerMethodArgumentResolver" />
<bean id="pageableResolver" class="org.springframework.data.web.PageableHandlerMethodArgumentResolver">
    <constructor-arg ref="sortResolver" />
</bean>

回答by Aalkhodiry

Or you can add this to user ApplicationContext

或者您可以将其添加到用户 ApplicationContext

<mvc:annotation-driven>
    <mvc:argument-resolvers>
        <bean class="org.springframework.data.web.PageableHandlerMethodArgumentResolver" />
    </mvc:argument-resolvers>
</mvc:annotation-driven>

it will work with Spring 4

它将与 Spring 4 一起使用