Skip to content
Advertisement

hide default parameters of page from json response | spring boot | Netflix Eurekka

I have JSON response from spring boot as,

{
    "content": [],
    "pageable": {
        "sort": {
            "sorted": true,
            "unsorted": false,
            "empty": false
        },
        "offset": 0,
        "pageNumber": 0,
        "pageSize": 10,
        "unpaged": false,
        "paged": true
    },
    "totalPages": 0,
    "totalElements": 0,
    "last": true,
    "size": 10,
    "number": 0,
    "sort": {
        "sorted": true,
        "unsorted": false,
        "empty": false
    },
    "numberOfElements": 0,
    "first": true,
    "empty": true
}

But I need this response as below,

{
  "content": [],
  "last": true,
  "totalPages": 0,
  "totalElements": 0,
  "size": 10,
  "number": 0,
  "sort": null,
  "first": true,
  "numberOfElements": 0
}

in brief, How can I modify the default parameters of returned response? is there any extra configuration I can set for that? thanks in advance

Advertisement

Answer

I guess you’re using spring data page so just create an implementation of JsonSerializer eg.

@JsonComponent
public class PageImplJacksonSerializer extends JsonSerializer<PageImpl> {

   @Override
   public void serialize(PageImpl page, JsonGenerator jsonGenerator, SerializerProvider serializers) throws IOException {
    
    jsonGenerator.writeStartObject();
    jsonGenerator.writeObjectField("content", page.getContent());
    jsonGenerator.writeBooleanField("first", page.isFirst());
    jsonGenerator.writeBooleanField("last", page.isLast());
    jsonGenerator.writeNumberField("page_number", page.getNumber());
    jsonGenerator.writeNumberField("elements_per_page", page.getNumberOfElements());
    jsonGenerator.writeNumberField("total_pages", page.getTotalPages());
    jsonGenerator.writeNumberField("total_elements", page.getTotalElements());
    jsonGenerator.writeEndObject();
  }
}
Advertisement