How to remove escape characters when JSON is added to model in spring rest controller
16,108
Instead of adding the string to model return JSON directly
@RequestMapping(value="/all")
public @ResponseBody String getJson(){
//Logic
return json;
}
Author by
Anand Sai Krishna
Updated on June 24, 2022Comments
-
Anand Sai Krishna almost 2 years
I am fetching JSON stored in DB (JSON is stored as a string in DB) and adding it to the model object in the controller.
@RequestMapping( method = RequestMethod.GET, value = "/all" ) public void getJson(HttpServletRequest httpServletRequest, Model model){ String json = serviceDao.getResponseJson(); System.out.println(json); //Output: {"Response":[{"Id":"1","Name":"GAD"},{"Id":"2","Name":"GBD"}],"Status":"Success"} model.addAttribute("result",json); }
But when I invoke the service from a browser, escape characters are added the response.
http://localhost:8080/MyApplication/all.json
{"result":"{\"Response\":[{\"Id\":\"1\",\"Name\":\"GAD\"},{\"Id\":\"2\",\"Name\":\"GBD\"}],\"Status\":\"Success\"}"}
Can you please help me on the way to send the JSON object to the client in a webservice without escape characters.