用途:
当接口文档(前端用的)中定义的属性名称和后端命令不完全一致时,不用要求前端或后端规定要哪一方的命名,@JsonProperty
可自定义后端返回属性的名称。即使前端要求的属性名称和后台数据库的不一样。
来源:com.fasterxml.jackson.annotation
用法:加在 VO 的属性上
原:
VO
import com.fasterxml.jackson.annotation.JsonProperty;
@Data
public class ProductVO {
private String categoryName;
private Integer categoryType;
private List<ProductInfoVO> foods;
}
Controller
@RequestMapping(value="list",method = RequestMethod.GET)
public Result<?> list() {
ProductVO productVO = new ProductVO();
return Result.success(productVO);
}
返回结果(使用前)
{
"code": 200,
"message": "成功",
"data": {
"categoryName": null,
"categoryType": null,
"foods": null
}
}
在属性上添加注解
@Data
public class ProductVO {
@JsonProperty("name")
private String categoryName;
@JsonProperty("type")
private Integer categoryType;
private List<ProductInfoVO> foods;
}
返回结果(使用后)
{
"code": 200,
"message": "成功",
"data": {
"foods": null,
"name": null,
"type": null
}
}
[2022-08-11] 版本:springboot 2.7.2