简体   繁体   English

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

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

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 并遇到此错误消息“No primary or default constructor found for interface 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.它是一个 Spring Boot 2.0 应用程序。 Thanks in advance!提前致谢!

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());
    }
}

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 @EnableSpringDataWebSupport as pointed by John Paul Moore's answer正如约翰保罗摩尔的回答所指出的那样,您可能也不需要@EnableSpringDataWebSupport

I had the same problem with the WebFlux app.我在使用 WebFlux 应用时遇到了同样的问题。 Spring Boot 2.4.0 misses the corresponding @EnableSpringDataWebSupport for the reactive apps but luckily provide working resolver implementation. Spring Boot 2.4.0 缺少响应式应用程序的相应@EnableSpringDataWebSupport ,但幸运的是提供了有效的解析器实现。 You can enable it in the following way:您可以通过以下方式启用它:

@Configuration
public class PageableWebFluxConfiguration implements WebFluxConfigurer {

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

}

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 之前缺少注释(您如何从客户端发送该数据?)。

Just to add to the answers already given regarding enabling @EnableSpringDataWebSupport using the annotation.只是添加到已经给出的关于使用注释启用 @EnableSpringDataWebSupport 的答案。 This should already be enabled with spring boot auto configuration.这应该已经通过 spring boot 自动配置启用。 You may need to check your configuration, or is this auto configuration class being excluded using java config or in application properties.您可能需要检查您的配置,或者是否使用 java config 或在应用程序属性中排除了此自动配置类。

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

For Non Spring Boot Code:对于非 Spring Boot 代码:

I was dealing with some existing spring mvc code base without spring boot, there was already a RequestMappingHandlerAdapter bean registered in the xmls, so I had to do this我正在处理一些没有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>

You can configure your test as:您可以将测试配置为:

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

See explanation https://www.javafixing.com/2021/12/fixed-request-is-not-being-executed-in.html见说明https://www.javafixing.com/2021/12/fixed-request-is-not-being-executed-in.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法实例化[org.springframework.data.domain.Pageable]:指定的类是接口 - Failed to instantiate [org.springframework.data.domain.Pageable]: Specified class is an interface 找不到接口 org.springframework.data.jpa.domain.Specification] 的主要或默认构造函数,其根本原因 - No primary or default constructor found for interface org.springframework.data.jpa.domain.Specification] with root cause 找不到接口 org.springframework.web.server.ServerWebExchange 的主要或默认构造函数 - No primary or default constructor found for interface org.springframework.web.server.ServerWebExchange mongodb org.springframework.data.authentication.UserCredentials未找到默认构造函数 - mongodb org.springframework.data.authentication.UserCredentials No default constructor found org.springframework.data.domain.PageImpl 无法反序列化,当我想使用带有注释 @Cacheable(spring cache) 的 findAll(Pageable pageable) 时? - org.springframework.data.domain.PageImpl can't be deserialized,when i want to use findAll(Pageable pageable) with annotation @Cacheable(spring cache)? org.springframework.beans.factory.BeanCreationException和org.springframework.beans.BeanInstantiationException,未找到默认构造函数 - org.springframework.beans.factory.BeanCreationException & org.springframework.beans.BeanInstantiationException,No default constructor found 找不到默认的构造函数; 嵌套异常org.springframework.test.context.TestContext。 <init> () - No default constructor found; nested exception org.springframework.test.context.TestContext.<init>() 没有找到接口 javax.ws.rs.core.UriInfo 的主要或单个公共构造函数 - 也没有找到默认构造函数 - No primary or single public constructor found for interface javax.ws.rs.core.UriInfo - and no default constructor found either 提交表单时没有找到接口 java.util.List 的主要或默认构造函数? - Receive No primary or default constructor found for interface java.util.List when submitting form? 找不到类 org.glassfish.jersey.media.multipart.FormDataContentDisposition 的主要或默认构造函数 - No primary or default constructor found for class org.glassfish.jersey.media.multipart.FormDataContentDisposition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM