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

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

No primary or default constructor found for interface org.springframework.data.domain.Pageable

javaspring-boot

提问by Sovan Misra

I was trying to implement Pageable in my RestController and running into issues with this error message "No primary or default constructor found for interface org.springframework.data.domain.Pageable"

我试图在我的 RestController 中实现 Pageable 并遇到此错误消息“找不到接口 org.springframework.data.domain.Pageable 的主要或默认构造函数”的问题

My Controller is

我的控制器是

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

What am I doing wrong here. It is a Spring Boot 2.0 Application. Thanks in advance!

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

采纳答案by Sovan Misra

I would suggest refactoring you controller to

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

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

I think you are missing an annotation before Pageable (how are you sending that data from client?).

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

回答by Clément Poissonnier

The selected solution is a workaround. You can make Spring resolve the parameters automatically using this configuration :

选定的解决方案是一种解决方法。您可以使用此配置使 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());
    }
}

回答by John Paul Moore

Just to add to the answers already given regarding enabling @EnableSpringDataWebSupport using the annotation. This should already be enabled with spring boot auto configuration. You may need to check your configuration, or is this auto configuration class being excluded using java config or in application properties.

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

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

回答by Mathieu Gemard

If you use Clément Poissonnier's solution, check if a configuration class do not override another one.

如果您使用Clément Poissonnier 的解决方案请检查配置类是否不覆盖另一个

I had the same problem and the solution below could not fix it:

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

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

I still had the message:

我仍然收到消息:

No primary or default constructor found for interface org.springframework.data.domain.Pageable

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

I then realized the project had a Swagger configuration class:

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

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

and that the above WebMvcConfig configuration was ignored.

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

The solution was to have only one configuration class:

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

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

    @Override
        public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
            argumentResolvers.add( new PageableHandlerMethodArgumentResolver());
        }
    }
}

You might also no need @EnableSpringDataWebSupportas pointed by John Paul Moore's answer

您可能也不需要@EnableSpringDataWebSupport正如约翰·保罗·摩尔的回答所指出的那样