Jersey and Jackson - a resource that modifies Jackson's output

I am currently using Jersey and Jackson to create a REST service. Right now, when the Resource method creates the / json application and returns the POJO, it correctly serializes the object in JSON and returns a response to the client.

Now I want to configure Jersey, so when a request arrives (for example, "indent"), I can say that Jackson will serialize JSON in a "nicer format, for example, indented." You can easily say that Jackson did this by setting up a JSON carpper with SerializationConfig.Feature.INDENT_OUTPUT.

The question is, how can I get a request on demand and use it to modify Jackson's output file?

+6
source share
1 answer

Something like that:

@GET @Path("path/to/rest/service") @Produces("application/json") public Response getSomething( @DefaultValue("false") @QueryParam("indent") boolean indent, ...) { ... if (indent) { objectMapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, true); } ... } 

Is this what you are looking for?

+1
source

Source: https://habr.com/ru/post/916484/


All Articles