7
votes

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?

1

1 Answers

1
votes

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?