7

I have a path that is necessary inform page, size, offset etc so I put a Pageable in method parameters:

Controller

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

I sent a request with Postman to application (localhost:8080/app/v1/orders?status=CANCELED&page=1&size=10) and received a NoSuchMethodException

Error

{
    "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"
}

Details

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]

The biggest problem is: OrderController needs a orderService bean so I can't create a default constructor with no parameters and let orderService being null.

Configuration

@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>

What I can do?

2
  • I've created a spring-boot app with all the details provided but haven't been able to reproduce the problem, so far it is working. Is the error occurring when you actually run the app, or does it occur when running a test? My guess is either PageableHandlerMethodArgumentResolver is not being registered or there are conflicting versions of spring-data on the classpath. Not sure it matters but what version of projectlombok are you using?
    – trf
    May 14, 2018 at 19:14
  • @trf I'm using org.projectlombok:lombok:1.16.20 May 15, 2018 at 18:11

3 Answers 3

5

you should use the @PagebleDefault annotation, e.g

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) {
2
  • 1
    Please explain why should he do that? May 14, 2018 at 15:55
  • 1
    You're right, i answer too quickly, and though it was the default missing, i'll update my answer. In fact the "EnableSpringDataWebSupport" annotation should be enought to get Pageable resolved
    – Vyncent
    May 14, 2018 at 16:46
2

I had the same problem. Like Kimosabe said, it's a Swagger problem. I fixed by adding the swagger resources on the resource handler. Apparently, Springfox Swagger and Spring Boot 2 has some issues. Here is the class:

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/");
    }

}
1

This is the Swagger problem, see this.

I did this for my project:

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

Hope it helps you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.