How to escape HTML in JSON
For now I have found following solution:I have added CharacterEscapes to the JsonFactory of the ObjectMapper class.Also I have changed way of writting JSON into response.Instead of
objectMapper.writeValue(response.getWriter(), myObject)
I'm doing this:
PrintWriter writer = response.getWriter();writer.print(String.valueOf(objectMapper.writeValueAsBytes(myObject));writer.flush();
And it works as I wanted.
I would suggest to use either of below.
1.You can use GSON library for this purpose.
Gson gson = new GsonBuilder().disableHtmlEscaping().create();
2.Use Apache commons StringEscapeUtils.escapeHtml("JSON string")
.