7

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?

William
  • 15,465
  • 8
  • 36
  • 32

1 Answers1

1

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?

Alex Stybaev
  • 4,623
  • 3
  • 30
  • 44
  • 1
    The issue is getting access to objectMapper. – William Jun 05 '12 at 18:53
  • Having the same problem, how to get the objectMapper? – Jurica Krizanic Apr 09 '15 at 10:22
  • 1
    object mapper is usually shared so you shouldn't change configuration on per request base. In http://stackoverflow.com/questions/18872931/custom-objectmapper-with-jersey-2-2-and-jackson-2-1 you can get idea how to have custom object mapper and how to get access to it. – Igor Cunko Mar 18 '16 at 18:30