Search code examples
javajsonspringresthateoas

Change word 'content' in json response created by CollectionModel


I want to know if it is possible to change the word 'content' in the JSON response. Links and content are being created by CollectionModel. JSON example:

{
    "links": [],
    "content": [
        {
            "id": 1,
            "name": "Pontar",
            "ownerName": "Pontar",
            "country": "Australia",
            "dateCreated": "28-09-2015"
        },
        {
            "id": 193074,
            "name": "Agri",
            "ownerName": "Agri",
            "country": "Mexico",
            "dateCreated": "28-08-2016"
        }
    ]
}

Method to create this response:

@GetMapping
public ResponseEntity<CollectionModel<FarmListDto>> getAllActiveFarms() {
    List<Farm> farms = farmsService.findActiveFarms();
    List<FarmListDto> dtoList = FarmListMapper.INSTANCE.map(farms);

    return ResponseEntity.ok(new CollectionModel<FarmListDto>(dtoList));
}

Thanks in advance.


Solution

  • ResponseEntity does serialize from your java-object, usually, it happens by some library such as jackson. There are ready solutions for change property name declarativity. For example:

    @JsonProperty("contentList")
    private List<...>content;
    

    If you use jackson just use the code above. Else try to find a similar solution with your deserialize library.