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

Sov*_*sra 7 java spring-boot

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

我的控制器是

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

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

Ale*_*ech 19

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

@Configuration
public class PageableWebFluxConfiguration implements WebFluxConfigurer {

    @Override
    public void configureArgumentResolvers(ArgumentResolverConfigurer configurer) {
        configurer.addCustomResolver(new ReactivePageableHandlerMethodArgumentResolver());
    }

}
Run Code Online (Sandbox Code Playgroud)


Lea*_*xto 12

您可以将测试配置为:

@BeforeEach
public void setup(){
  mockMvc = MockMvcBuilders.standaloneSetup(messageController)
    .setCustomArgumentResolvers(new PageableHandlerMethodArgumentResolver())
    .build();
}
Run Code Online (Sandbox Code Playgroud)

请参阅解释https://www.javafixing.com/2021/12/fixed-request-is-not-being-execute-in.html


Clé*_*ier 7

所选解决方案是一种解决方法。您可以使用以下配置使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());
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 只是上述完美解决方案的补充,请在 Spring Boot 2.3.* 及以上版本的配置中包含 @@EnableWebMvc,因为您的 WebMvcConfigurer 实现是由 @EnableWebMvc 启用的。它将允许将 Pageable 实例注入到根据请求参数自动创建的控制器方法中。 (2认同)

Mat*_*ard 7

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

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

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

我仍然收到消息:

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

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

@Configuration
@EnableSwagger2
public class SwaggerConfiguration extends WebMvcConfigurationSupport {
    // Swagger configuration...
}
Run Code Online (Sandbox Code Playgroud)

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

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

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

    @Override
        public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
            argumentResolvers.add( new PageableHandlerMethodArgumentResolver());
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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


小智 3

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

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

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