当前位置: 首页 > 知识库问答 >
问题:

使用带Spring Boot的Pageable

邢昊焜
2023-03-14

我有一个必要通知页面、大小、偏移量等的路径,所以我在方法参数中放置了一个可分页的:

控制器

@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@RestController
@RequestMapping("/orders")
public class OrderController {


    private final @lombok.NonNull OrderService orderService;

    @GetMapping()
    public ResponseEntity<Page<Order>> getOrdersByFilter(@RequestParam(value = "start", required = false) Date start,
                                                         @RequestParam(value = "end", required = false) Date end,
                                                         @RequestParam(value = "status", required = false) StatusType status,
                                                         @RequestParam(value = "delayed", required = false) Boolean delayed,
                                                         Pageable page) {
        OrderValidator.filtersAreValid(start, end, status, delayed);
        PedidoStatus orderStatus = StatusConverter.toDomain(status);
        Page<OrderES> orders = orderService.getOrdersByFilter(start, end, orderStatus, delayed, page);

        if (orders != null && !orders.getContent().isEmpty()) {
            Page result = new PageImpl(OrderESConverter.listToInterface(orders.getContent()), orders.getPageable(), orders.getTotalElements());
            return new ResponseEntity<>(result, HttpStatus.OK);
        }
        return new ResponseEntity<>(HttpStatus.OK);
    }
}

我和邮递员一起向应用程序发送了一个请求(本地主机:8080/app/v1/orders?状态=取消

错误

{
    "timestamp": 1526309013493,
    "status": 500,
    "error": "Internal Server Error",
    "message": "No primary or default constructor found for interface org.springframework.data.domain.Pageable",
    "path": "/app/v1/orders"
}

细节

2018-05-14 11:43:33.469 ERROR 10406 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[.[dispatcherServlet]      : Servlet.service() for servlet [dispatcherServlet] in context with path [/app/v1] threw exception [Request processing failed; nested exception is java.lang.IllegalStateException: No primary or default constructor found for interface org.springframework.data.domain.Pageable] with root cause

java.lang.NoSuchMethodException: org.springframework.data.domain.Pageable.<init>()
    at java.lang.Class.getConstructor0(Class.java:3082) ~[na:1.8.0_151]

最大的问题是:OrderController需要一个orderService bean,所以我不能创建一个没有参数的默认构造函数,让orderService为空。

配置

@EntityScan(basePackages = {"br.com.app.models.interfaces", "br.com.app.models.domains"})
@ComponentScan(basePackages = {"br.com.app.api.*"})
@EnableJpaRepositories
@SpringBootApplication
@EnableSpringDataWebSupport
public class OrderApplication {

    public static void main(String[] args) {
        ApplicationContext applicationContext = SpringApplication.run(OrderApplication.class, args);
    }

}

pom。xml(订单api)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>br.com.app</groupId>
    <artifactId>orders-api</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>orders-api</name>
    <description>Orders API</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.8.0</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.8.0</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk</artifactId>
            <version>1.9.0</version>
        </dependency>

        <dependency>
            <groupId>br.com.app</groupId>
            <artifactId>app-models</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>br.com.apputils</groupId>
            <artifactId>api-utils</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-elasticsearch</artifactId>
        </dependency>

        <!--<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>-->

        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
            <version>0.7.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <requiresUnpack>
                        <dependency>
                            <groupId>br.com.app</groupId>
                            <artifactId>app-models</artifactId>
                        </dependency>
                    </requiresUnpack>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>


</project>

pom。xml(应用程序)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>br.com.apputils</groupId>
    <artifactId>api-utils</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>api-utils</name>
    <description>Utils classes for APIs development</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk</artifactId>
            <version>1.9.0</version>
        </dependency>

        <dependency>
            <groupId>br.com.app</groupId>
            <artifactId>app-models</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
            <version>0.7.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>


</project>

pom。xml(应用模型)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>br.com.app</groupId>
    <artifactId>app-models</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.1-api</artifactId>
            <version>1.0.2.Final</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.2.13.Final</version>
            <exclusions>
                <exclusion>
                    <groupId>org.jboss.logging</groupId>
                    <artifactId>jboss-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.9.4</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.4</version>
        </dependency>

    </dependencies>

</project>

我能做什么?

共有3个答案

巫英纵
2023-03-14

这是斯威格问题,看这个。

我这样做是为了我的项目:

    @GetMapping("/orders")
    public ResponseEntity getAllOrders(@RequestParam(value = "page", defaultValue = "0", required = false) Integer page,
                                       @RequestParam(value = "size", defaultValue = "30", required = false) Integer size,
                                       @RequestParam(value = "sort", defaultValue = "orderNumber", required = false) String sort) {
        return execute(() -> service.getAllOrders(PageRequest.of(page, size, ASC, sort)));
    }

希望对你有帮助。

艾焱
2023-03-14

我也有同样的问题。就像Kimosabe所说的,这是一个傲慢的问题。我通过在资源处理程序上添加swagger资源进行了修复。显然,Springfox招摇和Spring Boot 2有一些问题。以下是课程:

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
        .addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/webjars/**")
        .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

}
晋安国
2023-03-14

您应该使用@PagebleDefault注释,例如

public ResponseEntity<Page<Order>> getOrdersByFilter(@RequestParam(value = "start", required = false) Date start,
                                                     @RequestParam(value = "end", required = false) Date end,
                                                     @RequestParam(value = "status", required = false) StatusType status,
                                                     @RequestParam(value = "delayed", required = false) Boolean delayed,
                                                     @PageableDefault(size = 10, direction = Sort.Direction.DESC, sort = "someField") Pageable page) {
 类似资料:
  • 有没有办法绕过授权?

  • 我尝试将Spring Boot与HikariDataSource配合使用,但出现了错误。断管怎么配置比较好还是用c3p0比较好?我正在使用这个配置 我使用: 和 springboot 版本 1.4.2.RELEASE

  • 我正在尝试将vaadin与spring(没有Spring Boot)和基于java注释的spring部分配置结合起来。 自动连接似乎适用于vaadin ui部分,但不适用于“自定义ui类”(例如,“公共类LoginScreen扩展自定义组件”)。我在SysOut上得到一个NPE或一个空对象。 此外,我注意到“@ComponentScan(base Packages={"net.myapp"})”没

  • 我尝试使用bytebuddy在springboot中实现aop。代码如下: springboot的监听器如下: 最后,在springboot启动中注册侦听器: 当我启动应用程序时,调试记录器显示良好。然而,当请求被处理时,aop不起作用。我做错了什么?我很困惑。。。

  • 首先要说的是,我一直在寻找解决方案,现在我非常绝望。 当由Spring Boot运行时,我无法从html页面访问css文件。 html。文件 应用JAVA 文件夹结构: 我尝试过将文件夹放入文件夹和/或删除addResourcesHandler,通过相对路径引用css和其他一些东西。似乎没有什么能解决这个问题。请告诉我,如果你试图解决这个问题,但没有找到解决方案,这样我就知道,我不会被忽视。

  • 本文向大家介绍解析SpringBoot @EnableAutoConfiguration的使用,包括了解析SpringBoot @EnableAutoConfiguration的使用的使用技巧和注意事项,需要的朋友参考一下 刚做后端开发的时候,最早接触的是基础的spring,为了引用二方包提供bean,还需要在xml中增加对应的包<context:component-scan base-packa

  • 使用SpringBoot创建了一个自定义jar,用于数据库表上的CRUD操作。其目的是使其成为一个实用程序jar,以便其他服务或应用程序可以将此jar用于该表上的任何操作。以下是我遵循的步骤: 1)。在REST SERVICE的pom.xml中添加此jar条目并成功构建。2)。在REST SERVICE的控制器内自动生成实用程序jar的服务类。 但是当我启动REST服务(服务是在spring bo

  • 我将使用Ehcache和Springboot。我只是想知道在处理大量请求时,什么会是最佳配置。 在直播期间,我们可能会收到超过30000 req/h。虽然在开发中,我们无法生成此方案。 你能帮我如何计算内存,以创建一个最佳配置,应在Prod中完美工作。 我检查了Postman中的响应大小,在Dev中为3-5 kb,因此我保留了以下配置,但不确定它会有多好。我们的内存大小为2GB(在prod中,我们