3

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();
        }
    }
3
  • I am not quite understand your solution. Are you returning JsonNode and passing it to ResponseBuilder#entity()? Wouldn't this get rid of quotations all together? Commented May 17, 2019 at 20:08
  • Correct. JsonNode object finally inserted in a POJO model that is used in ResponseBuilder. JsonNode is a class from Jackson library. Commented May 20, 2019 at 23:47
  • When you say inserted into a POJO, are you suggesting to create a POJO that has a JsonNode property, set by this object? Commented May 21, 2019 at 18:24

2 Answers 2

5

Why don't you annotate your getter with @JsonRawValue

public class Message {
    String json;

    public Message(String json) {
        this.json = json;
    }

    @JsonRawValue
    public String getJson() {
       return json;
    }

}

That

Sign up to request clarification or add additional context in comments.

1 Comment

this annotation was a truly life-savior for what was taking place last 2 hours...
1

You are converting your object to string in json format. Spring just returns a string value in json parameter. If you want to return json formated object not string do not convert your object to string( json ). Convert your json to Object type and delete the line below.

String json = buildJsonResponse(exception);

If you want to return custom json in string parameter convert your all object to json not only its variable.

You can just return string from your rest api with produces parameter like you added. produces = "application/json"

3 Comments

Which object to use for converting string? Not sure I understood the solution.
Change your Rest api response type to string and just return json object. IF you must return Response type DO NOT convert exception to json. Spring can convert that...
I can't change that. Response type a specific type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.