Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring Jackson mapper when using Dropwizard

I would like to use the Java 8 java.time with Jersey/Jackson in the context of a Dropwizard app. I understand I need to use jackson-modules-java8 and configure the mapper object.

But how do I configure Jersey's automagic mapper that deserialises the incoming JSON for me? I.e. where would I do mapper.registerModule(new JavaTimeModule());?

To illustrate the current situation here is an example class that represents the incoming JSON:

public class Example {
  // Want to use java.time instead
  private Date date;
  private final String ISO_OFFSET_DATE_TIME = "YYYY-MM-DD'T'HH:mm:ssZ";

  @JsonCreator
  public Example(@JsonProperty("date") 
                 @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = ISO_OFFSET_DATE_TIME) 
                 Date date) {
    this.date = date;
  }

  @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = ISO_OFFSET_DATE_TIME)
  public Date getDate() {
    return date;
  }
}

As you can see that uses the older Date API. The Jersey resources looks like the following:

@Path("/example")
@Consumes(MediaType.APPLICATION_JSON)
public class ExampleResource {
  @POST
  public void consume(Example example) {
    // Do stuff with example.date
  }
}
like image 343
Friedrich 'Fred' Clausen Avatar asked Nov 23 '18 03:11

Friedrich 'Fred' Clausen


People also ask

What is Jersey in Dropwizard?

What is Jersey? It is open source, production quality, framework for developing RESTful Web Services in Java that provides support for JAX-RS APIs and serves as a JAX-RS (JSR 311 & JSR 339) Reference Implementation.

What is Environment in Dropwizard?

A Dropwizard Environment consists of all the Resources, servlets, filters, Health Checks, Health, Jersey providers, Managed Objects, Tasks, and Jersey properties which your application provides. Each Application subclass implements a run method.

What is Dropwizard framework?

Dropwizard is an open source Java framework for developing ops-friendly, high-performance RESTful backends. It was developed by Yammer to power their JVM based backend. Dropwizard provides best-of-breed Java libraries into one embedded application package.

What is Mapper readValue?

The simple readValue API of the ObjectMapper is a good entry point. We can use it to parse or deserialize JSON content into a Java object. Also, on the writing side, we can use the writeValue API to serialize any Java object as JSON output.


1 Answers

JavaTimeModule is registered by default in Dropwizard 1.0.0 and above. For previous versions, the dropwizard-java8 bundle provided support for Java 8 features. Java 8 is the baseline for Dropwizard 1.0.0, and the bundle was merged into baseline.

Assuming you use Dropwizard 1.0.0 or above, if you still need to access the ObjectMapper, you can do it in your Application<T>:

  • in method void initialize(Bootstrap<T> bootstrap), via bootstrap.getObjectMapper()
  • in method abstract void run(T configuration, Environment environment), via environment.getObjectMapper()

That way, you can register other modules, or enable or disable Jackson features. Some of them impact how Java 8 types are serialized and deserialized.

like image 196
vin59 Avatar answered Sep 30 '22 08:09

vin59