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?
It provides two methods : Page findAll(Pageable pageable) – returns a Page of entities meeting the paging restriction provided in the Pageable object. Iterable findAll(Sort sort) – returns all entities sorted by the given options.
The Pageable implementation represents a set of pages to be printed. The Pageable object returns the total number of pages in the set as well as the PageFormat and Printable for a specified page.
In Spring Data JPA query results can be sorted in two ways: using an ORDER BY clause in a JPQL query. adding a parameter of type Sort to the query method.
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) {
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With