I have Spring endpoint which is supposed to return JSON so it can be collapsed/expanded via Chrome. Is there a way to tell Spring that string in message is actual Json representation, and no need to escape double quotes
Endpoint declaration:
@GET
@Path("/validate/{id}")
@Produces("application/json")
Response validate(@Context HttpServletRequest request, @PathParam("id") String id);
Endpoint implementation:
public Response validate(HttpServletRequest request, String id) {
try {
return validatorService.validate(request, String id);
} catch(Exception e) {
throw new MyCustomException(e);
}
}
Exception Handler:
public class ExceptionHandler implements ExceptionMapper {
@Override
public Response toResponse(MyCustomException exception) {
String json = buildJsonResponse(exception);
Message message = new Message(json);
return Response.status(ERROR_HTTP_STATUS_CODE).entity(response).build();
}
}
public class Message {
String json;
public Message(String json) {
this.json = json;
}
public String getJson() {
return json;
}
}
Response:
"json": "{ \"key\": \"value\" }"
Expected response:
"json": { "key": "value" }
Solution:
private JsonNode convertToJson(String json) {
ObjectMapper mapper = new ObjectMapper();
try {
return mapper.readTree(json);
} catch (IOException e) {
return NullNode.getInstance();
}
}
JsonNodeand passing it toResponseBuilder#entity()? Wouldn't this get rid of quotations all together?JsonNodeproperty, set by this object?