Spring Data不处理Pageable action参数创建 [英] Spring Data does not handle Pageable action argument creation

查看:180
本文介绍了Spring Data不处理Pageable action参数创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 */
    }
}

这是我的 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>

将此添加到我的 applicationContext.xml

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

我有以下错误:

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

Spring Data本身运行正常,JPA存储库正在运行。但到目前为止,我在控制器中手写了分页(我自己计算页面,手动创建 PageRequest 对象)。我想利用Spring Data网络附加功能,但由于某些原因它们对我不起作用...注册过时的 org.springframework.data.web.PageableArgumentResolver by手部分工作,但不完全,但仍然,我认为这甚至不应该是一个解决方案。

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 PageRequest objects 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.PageableArgumentResolver by hand partially made it working, but not completely, but still, I don't think this should even be a solution.

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?

推荐答案

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

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.

您必须手动将bean添加到 ConversionService RequestMappingHandlerMapping 。要么我们的开关,至少你的 DispatcherServlet 配置到Java Config。

You will have to add the beans manually to the ConversionService and your RequestMappingHandlerMapping. Either that our switch, at least your DispatcherServlet configuration to Java Config.

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

In XML you can configure additional argument-resolvers by using the <mvc:argument-resolvers /> tag. (This mimics the configuration from the 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>

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

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

<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>

这篇关于Spring Data不处理Pageable action参数创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆