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
}
}