Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jersey - Producing JSON Strings with Jackson

When I want to return JSON in my service, with the @Produces(MediaType.APPLICATION_JSON) annotation, it works well for POJO. (I am using Jackson, as explained in the Jersey's documentation).

But if I return a simple String, the result is not really what I expected. The String is returned but with no quotes to encapsulate it; thus, I am not able to parse it in my JavaScript client using JQuery.

Is this the normal behavior? Is there a way to add the quotes to the result?

like image 426
Nicolas B. Avatar asked Aug 05 '26 10:08

Nicolas B.


1 Answers

You can simply construct JSONObject:

 JSONObject myObject = new JSONObject();
 myObject.JSONObject myObject = new JSONObject();
 try {
   myObject.put("name", "Agamemnon");
   myObject.put("age", 32);
 } 
 catch (JSONException ex) {
   LOGGER.log(Level.SEVERE, "Error ...", ex);
 }
like image 112
Alex Stybaev Avatar answered Aug 08 '26 03:08

Alex Stybaev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!