我正在使用将jersey&jackson用于json的dropwizard。我的问题是,当我返回列表时,它没有指定根目录。
我有一个POJO课:
public class Company {
public String id;
public String name;
public String address;
}
这样我的资源就建立了:
@GET
@Path("/companies/all")
public List<Company> getAllCompanies(){
...
return companies;
}
我得到以下回应:
[{
"id": "01",
"name": "Yammer Corp",
"address": "1 Finite Loop"
},
{
"id": "02",
"name": "DropWizards Inc",
"address": "4 Magic Square, Olympus"
}]
虽然我想要的是以下内容:
{"Companies" :
[{
"id": "01",
"name": "Yammer Corp",
"address": "1 Finite Loop"
},
{
"id": "02",
"name": "DropWizards Inc",
"address": "4 Magic Square, Olympus"
}
]}
有任何想法吗?提前致谢。
最佳答案
您需要再创建一个包装List<Company>
的POJO
public class ApiResponse
{
private List<Company> Companies;
//Getter and Setter
//Public Constructor
}
GET方法中所需的更改是:
@GET
@Path("/companies/all")
public ApiResponse getAllCompanies(){
//Set your ApiResponse Object with the companies List.
ApiResponse apiResponse = new ApiResponse(companies);
return apiResponse;
}