It seems to me that you are relying on something to automatically map your Java objects to JSON - possibly Jackson. I personally do not prefer this method. Instead, I use Jettison and create my own mapping from Java to the Jettison JSONObject. Then I use JSONObject (or JSONArray) as an object. My return statement would be something like this:
return Response.ok().entity(myObjectAsJSON).build();
If returning a list of things, use a JSONArray instead of a JSONObject.
You will need a helper method to map a Java object to JSON.
public JSONArray deliverableListToJSON(List<Deliverable> deliverables) throws JSONException { JSONArray result = new JSONArray(); for(Deliverable deliverable : deliverables) { JSONObject deliverableJSON = new JSONObject(); deliverableJSON.put("importantValue", deliverable.getImportantValue()); result.put(deliverableJSON); } return result; }
This approach gives you more flexibility and does not force you to have public recipients and setters for all of your fields.
source share