Reputation: 21
Jersey by default convert single element returned to object . But the client side is expecting the data in json to be arraylist
Model class is LocationDetails
public class LocationDetails {
private String locationAccount;
private String locationName;
private String locationStreet;
private String locationPostcode;
}
other class LocationData which has arraylist of LocationDetails as
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class LocationData {
private ArrayList<LocationDetails> Locations;
}
and the controller class is using the following code to convert to 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);
}
The following code is returning json as LocationData : {LocationDetails : {"LocationAccount","10125"}}
whereas i want it to be like :
LocationData : {LocationDetails : [{"LocationAccount","10125"}]}
as the client side would expect the data to be inside list.
How to convert the type without changing the whole logic.I am using jersey.
Upvotes: 1
Views: 1473
Reputation: 1
I too had the same issue, after so many research i found that the problem is due to the jar files imported in the MyEclipse
(Persisted container). I have removed that. The same code works for me.
The json
format will be [{single element}].
Upvotes: 0
Reputation: 766
Add @XmlElementWrapper
annotation to your List
, that should do the trick. If you have further problems with Jersey, try first XML output to see what is being produced. If XML is ok, JSON will be too.
@XmlElement(name="locationDetail")
@XmlElementWrapper(name="locationDetails")
private List<LocationDetail> locationDetails;
Also name your class properties with first lowercase letter, please.
Upvotes: 1