在Spring Boot中使用Pageable

时间:2018-05-14 15:02:01

标签: spring spring-boot spring-data

我有一个必要的路径告知页面,大小,偏移等,所以我在方法参数中放了 Pageable

控制器

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

我向Postman发送了一个请求申请( localhost:8080 / app / v1 / orders?status = CANCELED&amp; page = 1&amp; size = 10 )并收到 NoSuchMethodException

错误

{
    "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为null。

配置

@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(orders-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(apputils)

<?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(app-models)

<?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 个答案:

答案 0 :(得分:2)

您应该使用@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) {

答案 1 :(得分:2)

我有同样的问题。就像Kimosabe所说的那样,这是一个Swagger问题。我通过在资源处理程序中添加大笔资源进行修复。显然,Springfox Swagger和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/");
    }

}

答案 2 :(得分:0)

这是Swagger问题,see this

我为我的项目做了这个:

    Unnamed: 0 gene scaf_name   start   end cov_depth   GC
428708  g66097.t1_0035_0035 scaffold_306390 1   695 32.2431654676259    0.389880952380952
342025  g50055.t1_0035_0035 scaffold_188566 15  954 7.062893081761009   0.351129363449692
214193  g28436.t1_0035_0042 scaffold_231066 1   842 25.9774346793349    0.348837209302326
400337  g60667.t1_0035_0035 scaffold_261197 309 656 15.873529411764698  0.353846153846154
224023  g30148.t1_0035_0042 scaffold_263686 10  414 23.2072538860104    0.34108527131782895
184987  g24481.t1_0035_0035 scaffold_65047  817 1593    27.7840552416824    0.533898305084746
249413  g34492.t1_0035_0035 scaffold_106432 1   511 3.2482544608223396  0.368318122555411
249418  g34493.t1_0035_0035 scaffold_106432 547 1230    3.2482544608223396  0.368318122555411
12667   g1120.t1_0035_0042  scaffold_2095   2294    2794    47.864745898359295  0.56203288490284
252797  g35042.t1_0035_0035 scaffold_108853 274 1276    20.269592476489 0.32735426008968604
255878  g36112.t1_0035_0042 scaffold_437464 1   540 74.8252551020408    0.27884615384615397
40058   g4082.t1_0035_0042  scaffold_11195  579 1535    33.4396168320219    0.48487467588591204
271053  g39343.t1_0035_0042 scaffold_590976 1   290 19.6666666666667    0.38636363636363596
89911   g10947.t1_0035_0035 scaffold_21433  1735    2373    32.4222503160556    0.408571428571429

希望它对你有所帮助。