【问题标题】:No primary or default constructor found for interface org.springframework.data.domain.Pageable找不到接口 org.springframework.data.domain.Pageable 的主要或默认构造函数
【发布时间】:2019-02-20 15:43:27
【问题描述】:

我试图在我的 RestController 中实现 Pageable 并遇到了这个错误消息“No primary or default constructor found for interface org.springframework.data.domain.Pageable”的问题

我的控制器是

@GetMapping("/rest/category/all/page")
public Page<ItemCategory> getAllItemCategoryByPage(Pageable pageable){
    Page<ItemCategory> categories = itemCategoryService.getAllItemCategoriesByPageable(pageable);
    return categories;
}

我在这里做错了什么。它是一个 Spring Boot 2.0 应用程序。 提前致谢!

【问题讨论】:

    标签: java spring-boot


    【解决方案1】:

    所选解决方案是一种解决方法。您可以使用此配置使 Spring 自动解析参数:

    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
    import org.springframework.data.web.config.EnableSpringDataWebSupport;
    import org.springframework.web.method.support.HandlerMethodArgumentResolver;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    
    import java.util.List;
    
    @Configuration
    @EnableSpringDataWebSupport
    public class WebMvcConfig implements WebMvcConfigurer {
        @Override
        public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
            argumentResolvers.add( new PageableHandlerMethodArgumentResolver());
        }
    }
    

    【讨论】:

    • 只是上述完美解决方案的补充,请在 spring boot 2.3.* 及更高版本的配置中包含@@EnableWebMvc,因为您的 WebMvcConfigurer 实现由@EnableWebMvc 启用。它将允许将 Pageable 实例注入到从请求参数自动创建的控制器方法中。
    【解决方案2】:

    如果你使用Clément Poissonnier's solution检查一个配置类是否不会覆盖另一个

    我遇到了同样的问题,下面的解决方案无法解决:

    @Configuration
    @EnableSpringDataWebSupport
    public class WebMvcConfig implements WebMvcConfigurer {
        @Override
        public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
            argumentResolvers.add( new PageableHandlerMethodArgumentResolver());
        }
    }
    

    我仍然有消息:

    找不到接口的主要或默认构造函数 org.springframework.data.domain.Pageable

    然后我意识到该项目有一个 Swagger 配置类

    @Configuration
    @EnableSwagger2
    public class SwaggerConfiguration extends WebMvcConfigurationSupport {
        // Swagger configuration...
    }
    

    并且上面的 WebMvcConfig 配置被忽略了

    解决方案是只有一个配置类:

    @Configuration
    @EnableSwagger2
    public class WebMvcConfig extends WebMvcConfigurationSupport {
        // Swagger configuration...
    
        @Override
            public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
                argumentResolvers.add( new PageableHandlerMethodArgumentResolver());
            }
        }
    }
    

    你可能也不需要John Paul Moore's answer所指出的@EnableSpringDataWebSupport

    【讨论】:

    • 感谢您分享此内容。你拯救了我的一天。 ^^
    【解决方案3】:

    我在使用 WebFlux 应用时遇到了同样的问题。 Spring Boot 2.4.0 缺少响应式应用程序对应的@EnableSpringDataWebSupport,但幸运的是提供了有效的解析器实现。您可以通过以下方式启用它:

    @Configuration
    public class PageableWebFluxConfiguration implements WebFluxConfigurer {
    
        @Override
        public void configureArgumentResolvers(ArgumentResolverConfigurer configurer) {
            configurer.addCustomResolver(new ReactivePageableHandlerMethodArgumentResolver());
        }
    
    }
    

    【讨论】:

      【解决方案4】:

      只是添加到已经给出的关于使用注释启用@EnableSpringDataWebSupport 的答案。这应该已经通过 spring boot 自动配置启用。您可能需要检查您的配置,或者是否使用 java config 或在应用程序属性中排除了此自动配置类。

         org.springframework.boot.autoconfigure.data.web.SpringDataWebAutoConfiguration
      

      【讨论】:

        【解决方案5】:

        我建议将你的控制器重构为

        public List<ItemCategory> getAllItemCategoryByPage(@RequestParam("page") int pageIndex, 
                                                           @RequestParam("size") int pageSize){
             return itemCategoryService
                           .getAllItemCategoriesByPageable(PageRequest.of(pageIndex, pageSize)).getContent();
        }
        

        我认为您在 Pageable 之前缺少注释(您如何从客户端发送该数据?)。

        【讨论】:

        • localhost:8080/rest/category/all/page?page=1&size=1 这就是我调用控制器的方式
        • 好的,谢谢!有效!只需进行一项更改,而不是使用已弃用的 new PageRequest(page, size),我使用了Pageable pageable = PageRequest.of(page, size);
        • 谢谢,我也用过这个解决方案,只是添加了默认值以确保安全。
        【解决方案6】:

        对于非 Spring Boot 代码:

        我正在处理一些没有spring boot的现有spring mvc代码库,xmls中已经注册了一个RequestMappingHandlerAdapter bean,所以我不得不这样做

            <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
                ...
                <property name="customArgumentResolvers">
                    <list>
                        <bean class="org.springframework.data.web.PageableHandlerMethodArgumentResolver">
                        </bean>
                    </list>
                </property>
            </bean>
        

        【讨论】:

          猜你喜欢
          • 2020-11-17
          • 2019-06-23
          • 2018-11-13
          • 2019-07-06
          • 2018-08-16
          • 2019-12-18
          • 2019-05-06
          • 2012-05-12
          • 2022-01-18
          相关资源
          最近更新 更多