【发布时间】:2016-12-14 21:57:54
【问题描述】:
我有一个基本的 Rest Controller,它以 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数组?
标签: java json spring rest controller