Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jersey & Jackson - Resource modifying Jackson output

I'm currently using Jersey & Jackson for creating REST service. Right now when a Resource method produces application/json and is returned a POJO, it properly serializes the object into JSON and returns the response to the client.

What I'm looking to do now is setup Jersey so when a queryparam comes in (lets say "indent"), I can tell Jackson to serialize the JSON in a "prettier format, aka indented". You can easily tell Jackson to do this by configuring the JSON mapper with SerializationConfig.Feature.INDENT_OUTPUT.

The question is, how do I on a per-request basis take a queryparam and use that to modify Jackson's output?

like image 620
William Avatar asked May 23 '12 18:05

William


1 Answers

Something like this:

@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 what you looking for?

like image 94
Alex Stybaev Avatar answered Oct 17 '22 03:10

Alex Stybaev