我有一个基本的休息控制器返回模型列表中JSON到客户端:爪哇 - 弹簧复位JSON对象/数组
@RestController
public class DataControllerREST {
@Autowired
private DataService dataService;
@GetMapping("/data")
public List<Data> getData() {
return dataService.list();
}
}
在这种格式返回数据:
[
{
"id": 1,
"name": "data 1",
"description": "description 1",
"active": true,
"img": "path/to/img"
},
// etc ...
]
那是伟大的开始,但我想过这个返回格式的数据:
[
"success": true,
"count": 12,
"data": [
{
"id": 1,
"name": "data 1",
"description": "description 1",
"active": true,
"img": "path/to/img"
},
{
"id": 2,
"name": "data 2",
"description": "description 2",
"active": true,
"img": "path/to/img"
},
]
// etc ...
]
,但我不能确定回合这个问题,因为我不能返回任何类作为JSON ...任何人有建议或意见?
问候和感谢!
当你的JSON以“[”开头,这意味着它的数组。你实际上是指一个数组还是你的意思是一个有'data'数组的对象('{}')? – Adam