I am developing Rest Apis using Spring mvc 5.0.8 and jackson 2.9.8
I want to set default pagination against every request, if pagination property is not present in client's json request.
I am achieving that by creating Pagination object from HttpRequestEntity class constructor,but If pagination request is present in json request,then jackson creating one more Pagination object by calling default constructor of Pagination class before calling setter method(setPagination(Pagination pagination)) of HttpRequestEntity class.
JackSon neither will create property object nor will hit the property setter method,if property is not present in JSON request
I also tried this one using Interceptor prepreHandle
request.setAttribute("pagination", new Pagination());
But that will not update HttpServletRequest's body,I have to receive that property with Controller's parameter which should be annotated with @RequestAttribute.
As per my under standing HttpMessageConverter only maps HttpServletRequest body with spring annotation @RequestBody.
Json Request
{
//not sending pagination
/*
"pagination":{
"offset":0,
"limit:50
},
*/
"data":{
"requestUrl":"some value"
...
},
"timeStamp":"Mon Sep 23 2019 02:15:47 GMT+0530 (India Standard Time)"
}
Controller
@RestController
@RequestMapping("/api/v1")
public class LocaldbController {
@Autowired
private LocaldbService localdbService;
@PostMapping("/apidetails/filter")
public void getAllApiDetails(@RequestBody HttpRequestEntity<ApiDetailsFilterDto> requestEntityDto) {
System.out.println(requestEntityDto.getPagination().getLimit());
}
}
public class HttpRequestEntity<T> {
public Pagination pagination;
private T data;
private String timeStamp;
public HttpRequestEntity() {
this.pagination = new Pagination(); //To set Default value
}
public Pagination getPagination() {
return pagination;
}
public T getData() {
return data;
}
public String getTimeStamp() {
return timeStamp;
}
public void setTimeStamp(String timeStamp) {
this.timeStamp = timeStamp;
}
public void setPagination(Pagination pagination) {
this.pagination = pagination;
}
}
public class Pagination {
private int offset = 0;
private int limit = 50;
public Pagination() {
System.out.println("pagination");
}
public int getOffset() {
return offset;
}
public int getLimit() {
return limit;
}
public void setOffset(int offset) {
this.offset = offset;
}
public void setLimit(int limit) {
this.limit = limit;
}
}
So anyone can tell me,how to set Default values to Properties if properties are not present in JSON request.
Thanks for your attention. I’m looking forward to your reply.