Dropwizard / Jersey returns the Java list as JSON without a root node

I'm using the dropwizard using plain knit fabric for Jason My problem is that when I return a list, it does not specify a root

I have a POJO course:

public class Company { 
public String id; 
public String name; 
public String address; 
}

My resource settings are as follows:

@GET
@Path("/companies/all")
public List<Company> getAllCompanies(){
    ...
    return companies;
}

I received the following reply:

[{
"id": "01","name": "Yammer Corp","address": "1 Finite Loop"
},{
"id": "02","name": "DropWizards Inc","address": "4 Magic Square,Olympus"
}]

Although what I want is as follows:

{"Companies" : 
[{
"id": "01",Olympus"
}
]}

Any ideas? Thank you in advance

Solution

You need to create another POJO wrapper list < Company >

public class ApiResponse
{
   private List<Company> Companies;
   //Getter and Setter
   //Public Constructor
}

The required changes in your get method are:

@GET
@Path("/companies/all")
public ApiResponse getAllCompanies(){
    //Set your ApiResponse Object with the companies List.
    ApiResponse apiResponse = new ApiResponse(companies);
    return apiResponse;
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>