我希望JSON响应采用某种格式。
这是我的公司,LocationInfo看起来像
public class Company {
private List<LocationInfo> companies;
// gettts and setters
}
公共类LocationInfo {
private String localCompanyID;
private String localCompanyName;
// gettts and setters
}
我的控制器如下所示
@GetMapping(value = "/url", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<LocationInfo> getLocationMaster() {
ResponseEntity response;
List<LocationInfo> companies = getAllCompanies();
// some code goes here
response = new ResponseEntity<>(localCompanies, HttpStatus.OK);
return response;
}
这就是我要得到的
[
{
"localCompanyID": "33",
"localCompanyName": "Company-A"
},
{
"localCompanyID": "45",
"localCompanyName": "Company-B"
}
]
我希望答复如下。
{
"localCompanies": [
{
"localCompanyID": "33",
"localCompanyName": "Company-A"
},
{
"localCompanyID": "45",
"localCompanyName": "Company-B"
}
]
}
谢谢。
答案 0 :(得分:1)
创建一个响应DTO类并将您的列表添加到其中:
@AllArgsConstructor(staticName = "of")
class LocalCompaniesResponseDto {
List<LocationInfo> localCompanies;
}
new ResponseEntity<>(LocalCompaniesResponseDto.of(localCompanies), HttpStatus.OK)