Reputation: 4393
I am trying to build a 'list all with filters' API. What I am trying to do is get the parameters from which to filter by as query parameters and then pass them forward to criteria API. I have created a Filter object in which i have added all the fields with which I would want to filter my results by, but passing the object as query parameter it is not getting mapped to the java object correctly and the fields are still null. Attaching my code snippet below.
public Page<ForecastResponse> listForecast(ForecastFilterCriteria forecastFilterCriteria,@NotNull final Pageable pageable) {
logger.info("Got the following as pageable request "+pageable.toString());
logger.info("Got the following filters "+forecastFilterCriteria.toString());
return listForecastUsecase.forecasts( forecastFilterCriteria,pageable);
}
@Data
@Builder
public class ForecastFilterCriteria {
private String forecastId;
private String forecastName;
private String forecastGroupName;
private String latestVersion;
private ForecastStatus status;
private PlanHorizon planHorizon;
private Interval interval;
private String forecasterName;
private Long createdAt;
private Long modifiedAt;
private String createdBy;
private String modifiedBy;
}
Curl commands I have tried so far
1. curl http://localhost:8443/v2/forecast/?page=2&size=5
2. curl http://localhost:8443/v2/forecast/?forecasterName=Pradeep&page=1&size=10
3. curl http://localhost:8443/v2/forecast/?forecastFilterCriteria={"forecasterName":"Pradeep"}&page=1&size=10
4. curl http://localhost:8443/v2/forecast/?forecasterName=Pradeep&forecastId=myId&page=1&size=10
My Observations :
• Only the first parameter is what gets initialized correctly ie if I pass parameters as in the 4th curl command, only the name gets initialized correctly, forecastId remains null and page and size parameters are taking what I believe default values of 0 and 20. What am I missing?
Edit : Attaching the screenshot
Upvotes: 1
Views: 1867
Reputation: 121
You should use correct field names in query. Field with name 'forecasterName' doesn't exists.
query example:
http://localhost:8080/?status=OK&forecastId=1&forecastName=name&createdBy=owner&page=2&size=11&sort=a,desc&sort=b
response example:
{
"criteria": {
"forecastId": "1",
"forecastName": "name",
"forecastGroupName": null,
"latestVersion": null,
"status": "OK",
"forecasterName": null,
"createdAt": null,
"modifiedAt": null,
"createdBy": "owner",
"modifiedBy": null
},
"pageable": {
"sort": {
"empty": false,
"sorted": true,
"unsorted": false
},
"offset": 22,
"pageSize": 11,
"pageNumber": 2,
"paged": true,
"unpaged": false
}
}
filled objects (pageable and criteria) in controller (debug mode):
application:
package com.example.demo;
import lombok.Builder;
import lombok.Data;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@SpringBootApplication
public class DemoApplication {
public enum ForecastStatus {
OK,
UNKNOWN
}
@Data
@Builder
public static class ForecastFilterCriteria {
//
//query parameters names should be equal to fields names
//
private String forecastId;
private String forecastName;
private String forecastGroupName;
private String latestVersion;
private ForecastStatus status;
private String forecasterName;
private Long createdAt;
private Long modifiedAt;
private String createdBy;
private String modifiedBy;
}
@RestController
@RequestMapping("/")
public static class Controller {
@GetMapping
public Map<String, Object> get(ForecastFilterCriteria criteria, Pageable pageable) {
HashMap<String, Object> values = new HashMap<>();
values.put("criteria", criteria);
values.put("pageable", pageable);
return values;
}
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>demo</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
project structure:
Upvotes: 1