java 无法实例化 Pageable bean

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30057262/
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-11-02 16:22:08  来源:igfitidea点击:

Failed to instantiate Pageable bean

javaspringspring-mvcspring-data-jpa

提问by Dimon

I use Spring 4.1.6.RELEASE and Spring Data Jpa 1.8.0.RELEASE. I have a problem with org.springframework.data.domain.Pageable bean creation. It is used in my controller:

我使用 Spring 4.1.6.RELEASE 和 Spring Data Jpa 1.8.0.RELEASE。我在创建 org.springframework.data.domain.Pageable bean 时遇到问题。它在我的控制器中使用:

@Controller
public class ItemsController {

    @Autowired
    ProductService itemsService;

    @RequestMapping(value = "/openItemsPage")
    public String openItemsPage() {
        return "items";
    }

    @RequestMapping(value = "/getItems", method = RequestMethod.GET)
    @ResponseBody
    public Item[] getItems(Pageable pageable) {

        return itemsService.getItems(pageable);
    }
}

Also I have a next xml configurations in my application context:

此外,我的应用程序上下文中有一个下一个 xml 配置:

<context:component-scan base-package="com.mobox.controller" />

<mvc:annotation-driven>
    <mvc:argument-resolvers>
        <beans:bean id="sortResolver"
                class="org.springframework.data.web.SortHandlerMethodArgumentResolver" />
        <beans:bean
                class="org.springframework.data.web.PageableHandlerMethodArgumentResolver">
            <beans:constructor-arg ref="sortResolver" />
        </beans:bean>
    </mvc:argument-resolvers>
</mvc:annotation-driven>

And finaly I do A next requsr from the client:

最后我做了一个来自客户的下一个请求:

   $.ajax({
        type: "GET",
        url: "getProducts?page=0&size=100",
        .....

In tomcat log I see next:

在 tomcat 日志中我看到下一个:

    SEVERE: Servlet.service() for servlet [appServlet] in context with path [/a2delivery-web] threw exception [Request processing failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.domain.Pageable]: Specified class is an interface] with root cause
org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.domain.Pageable]: Specified class is an interface
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:101)
    at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.createAttribute(ModelAttributeMethodProcessor.java:137)
    at org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.createAttribute(ServletModelAttributeMethodProcessor.java:80)
    at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:106)
    at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:77)
    at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:162)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:129)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
    ....................

Please help me to resolve this issue, thanks!

请帮我解决这个问题,谢谢!

回答by Oliver Drotbohm

The easiest way to get this working is to set @EnableSpringDataWebSupportin your configuration. Alternatively, in a pure XML based configuration, declare SpringDataWebConfigurationas Spring bean.

使此工作最简单的方法是@EnableSpringDataWebSupport在您的配置中进行设置。或者,在基于纯 XML 的配置中,声明SpringDataWebConfiguration为 Spring bean。

That will make sure the necessary HandlerMethodArgumentResolverwill be registered correctly.

这将确保必要的HandlerMethodArgumentResolver将被正确注册。

回答by Tom Van Rossom

Add the following to you're test class:

将以下内容添加到您的测试类:

@Inject
private PageableHandlerMethodArgumentResolver pageableArgumentResolver;

PageableHandlerMethodArgumentResolver

PageableHandlerMethodArgumentResolver

and configure it during MockMvc setup:

并在 MockMvc 设置期间对其进行配置:

@Before
public void setup() {
    ...
    this.mockMvc = MockMvcBuilders.standaloneSetup(resource)
        .setCustomArgumentResolvers(pageableArgumentResolver)
        .build();
}

回答by mengjiann

just to add on to Tom Van Rossom's reply, if you use @RunWith(MockitoJUnitRunner.class), you can create an instance of PageableHandlerMethodArgumentResolver when you initialize the mockMvc (like what Loren mentioned).Eg

只是为了补充 Tom Van Rossom 的回复,如果您使用 @RunWith(MockitoJUnitRunner.class),您可以在初始化 mockMvc 时创建一个 PageableHandlerMethodArgumentResolver 实例(就像 Loren 提到的那样)。例如

mockMvc = MockMvcBuilders.standaloneSetup(restController)
            .setCustomArgumentResolvers(new PageableHandlerMethodArgumentResolver())
            .build();

回答by Mehdi

Alternative way besides other answers which might work for you.

除了可能对您有用的其他答案之外的替代方法。

@Configuration
@ComponentScan(basePackages = {"my.package1", "my.package2"})
@EnableWebMvc
public class SpringMVCRestConfiguration extends WebMvcConfigurerAdapter {

    //Adding the resolver for Pagination and Sorting Resolver
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(paginationResolver());
        argumentResolvers.add(sortResolver());
    }

    //Inject the sortResolver
    @Bean
    public SortHandlerMethodArgumentResolver sortResolver() {
        SortHandlerMethodArgumentResolver argumentResolver = new SortHandlerMethodArgumentResolver();
        return argumentResolver;
    }

    //Inject the Pagination Resolver
    @Bean
    public PageableHandlerMethodArgumentResolver paginationResolver() {
        PageableHandlerMethodArgumentResolver argumentResolver = new PageableHandlerMethodArgumentResolver();
        argumentResolver.setMaxPageSize(10);
        argumentResolver.setOneIndexedParameters(true);
        return argumentResolver;
    }
}

回答by yunbugg

Pageable used in the getItems method is an interface and spring will not be able to instantiate it.

getItems 方法中使用的 Pageable 是一个接口,spring 将无法实例化它。

You can either

你可以

  1. Provide an Java bean style implementation of Pageable and used that instead of Pageable in the getItems method.
  2. Or use the ModelAttribute to create a Pageable object(for example, PageRequest). Here is a link for more details on ModelAttribute: What is @ModelAttribute in Spring MVC?
  1. 提供 Pageable 的 Java bean 样式实现并在 getItems 方法中使用它而不是 Pageable。
  2. 或者使用 ModelAttribute 创建一个 Pageable 对象(例如,PageRequest)。这是有关 ModelAttribute 的更多详细信息的链接:What is @ModelAttribute in Spring MVC?