首页 > 解决方案 > 找不到接口 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 应用程序。提前致谢!

标签: javaspring-boot

解决方案


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

推荐阅读