在 CollectionModel 创建的 json 响应中更改单词“内容”

问题描述

我想知道是否可以更改 JSON 响应中的“内容”一词。 CollectionModel 正在创建链接内容。 JSON 示例:

{
    "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"
        }
    ]
}

创建此响应的方法

@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));
}

提前致谢。

解决方法

ResponseEntity 确实从您的 java 对象序列化,通常,它发生在某些库中,例如 jackson。有现成的更改属性名称声明的解决方案。例如:

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

如果您使用 jackson,只需使用上面的代码。否则,请尝试使用您的反序列化库找到类似的解决方案。