Jersey 默认将单个元素转换为 object 。但是客户端期望json中的数据是arraylist
模型类是 LocationDetails
public class LocationDetails {
private String locationAccount;
private String locationName;
private String locationStreet;
private String locationPostcode;
}
其他具有 LocationDetails 数组列表的类 LocationData 作为
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class LocationData {
private ArrayList<LocationDetails> Locations;
}
控制器类使用以下代码转换为 JSON:
//Getting location and item along with barcodes
@POST
@Path("/getLocationAndItemData")
@Produces(MediaType.APPLICATION_JSON)
public LocationResponse getAlllocations(){
ArrayList<LocationDetails> locationDetailList = new ArrayList<LocationDetails>();
LocationDetails details = new LocationDetails();
//setting location account
details.setLocationAccount("10125");
locationDetailList.add(details);
}
以下代码将 json 返回为LocationData : {LocationDetails : {"LocationAccount","10125"}}
而我希望它像:
LocationData : {LocationDetails : [{"LocationAccount","10125"}]}
因为客户端希望数据在列表中。
如何在不改变整个逻辑的情况下转换类型。我正在使用球衣。