![](/img/trans.png)
[英]Failed to instantiate [org.springframework.data.domain.Pageable]: Specified class is an interface
[英]No primary or default constructor found for interface org.springframework.data.domain.Pageable
我试图在我的 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 应用程序。 提前致谢!
选择的解决方案是一种解决方法。 您可以使用此配置使 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());
}
}
如果您使用Clément Poissonnier 的解决方案,请检查配置类是否不覆盖另一个配置类。
我遇到了同样的问题,下面的解决方案无法解决:
@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());
}
}
}
正如约翰保罗摩尔的回答所指出的那样,您可能也不需要@EnableSpringDataWebSupport
我在使用 WebFlux 应用时遇到了同样的问题。 Spring Boot 2.4.0 缺少响应式应用程序的相应@EnableSpringDataWebSupport
,但幸运的是提供了有效的解析器实现。 您可以通过以下方式启用它:
@Configuration
public class PageableWebFluxConfiguration implements WebFluxConfigurer {
@Override
public void configureArgumentResolvers(ArgumentResolverConfigurer configurer) {
configurer.addCustomResolver(new ReactivePageableHandlerMethodArgumentResolver());
}
}
我建议将您的控制器重构为
public List<ItemCategory> getAllItemCategoryByPage(@RequestParam("page") int pageIndex,
@RequestParam("size") int pageSize){
return itemCategoryService
.getAllItemCategoriesByPageable(PageRequest.of(pageIndex, pageSize)).getContent();
}
我认为您在 Pageable 之前缺少注释(您如何从客户端发送该数据?)。
只是添加到已经给出的关于使用注释启用 @EnableSpringDataWebSupport 的答案。 这应该已经通过 spring boot 自动配置启用。 您可能需要检查您的配置,或者是否使用 java config 或在应用程序属性中排除了此自动配置类。
org.springframework.boot.autoconfigure.data.web.SpringDataWebAutoConfiguration
对于非 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>
您可以将测试配置为:
@BeforeEach
public void setup(){
mockMvc = MockMvcBuilders.standaloneSetup(messageController)
.setCustomArgumentResolvers(new PageableHandlerMethodArgumentResolver())
.build();
}
见说明https://www.javafixing.com/2021/12/fixed-request-is-not-being-executed-in.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.