Reputation: 11
Please help me to resolve the problem of removing the \n and \" in the json response. My code is:
I have created the below json string using the code...
String json = new GsonBuilder().serializeNulls().setPrettyPrinting().disableHtmlEscaping().create().toJson(data);
Content of Json created above looks like this :
String json = "{\n \"transactionDetail\": {\n \"transactionID\": \"123456\",\n \"transactionTimestamp\": \"2015-07-08T11:44:19Z\",\n \"inLanguage\": \"en-US\",\n \"serviceVersion\": \"1.0\",\n \"additionalDetails\": null\n }} ";
Return javax.ws.rs.core.Response:
return Response.status(Status.OK).entity(json).type(MediaType.APPLICATION_JSON).build();
I am seeing the output in swagger/postman my rest clients, the same way as how the content present in the above string variable json, the new lines and quotes are not replaced properly. If i put a sysout , in the command prompt the string getting printed neatly with proper newline and quotes. How to send back the response where the new line and double quote will get replaced properly in swagger or postman.
Upvotes: 0
Views: 2525
Reputation: 388
Try removing the setPrettyPrinting(). That would remove all the formatting i.e. the \n. As for the string that is generated, check what is the type of the variable 'data'. If the 'data' is actually a json string and you are using Gson to serialize it, it will escape all the quotes in that json string again i.e. double serializing. 'data' should be a java object like a hashmap for example, then you can serialize into json. If none of that works, try using the Jackson Json library.
Upvotes: 1