json - Springboot - 如何在 JSON 响应中命名响应数组和对象
问题描述
我有带有控制器的 Springboot REST API 应用程序,例如:
@GetMapping()
public List<CarResponse> getCars() {
List<CarResponse> cars= carService.getCars();
return cars;
}
我的 CarResponse 看起来像:
public class CarResponse {
private int id;
private String type;
private String model;
}
这工作正常;但是,返回的 JSON 包含一个匿名 JSON 对象的 JSON 匿名数组,例如:
[
{
"id": 1,
"type": "SUV",
"model": "Toyota"
},
{
"id": 2,
"type": "SPORT",
"model": "Porsche"
},
...
]
如何获得由命名数组和对象组成的响应,以便响应 JSON 看起来像
{
"cars": [
"car": {
"id": 1,
"type": "SUV",
"model": "Toyota"
},
"car": {
"id": 2,
"type": "SPORT",
"model": "Porsche"
},
...
]
}
解决方案
推荐阅读
- python - 自动化我创建 virtualenv 和 django 的任务
- java - 为什么子类对象引用父类值
- laravel - 当我使用 laravel 重新加载页面时,我想显示第一类产品?
- r - 有没有办法在 R 中将变量存储为数字但显示为 date9 格式?
- c# - 如何抑制用户对 ListBox 控件的操作
- angular - 如何在ag-grid中将特定列的部分标题名称更改为小写
- python - pd.ExcelWriter,追加日期时间但得到:未知或不支持的日期时间类型
- node.js - 尝试在arrayOfObject中使用分页(每页10条记录)
- karate - 如何在空手道 UI 自动化中使用 if-else
- reactjs - Material Ui TextField 移除底部边框