I created a RESTful web service using JAX-RS method annotations:
@GET
@Path(\"/test\")
@Produces(MediaType.APPLICATION_JSON)
public MyThing test()
{
MyTh
I've never use LocalDateTime
before, so I decided to do some testing. Here are my findings:
Jersy 2.13 and this provider (works out the box with no extra configuration)
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>${jersey.version}</version>
</dependency>
Jersey 2.13 with this provider (has support for JAXB annotation - dependency on jackson-module-jaxb-annotations), with custom adapter
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>${jersey.version}</version>
</dependency>
public class LocalDateTimeAdapter extends XmlAdapter<String, LocalDateTime> {
@Override
public LocalDateTime unmarshal(String s) throws Exception {
return LocalDateTime.parse(s);
}
@Override
public String marshal(LocalDateTime dateTime) throws Exception {
return dateTime.toString();
}
}
// Getter for model class
@XmlJavaTypeAdapter(LocalDateTimeAdapter.class)
public LocalDateTime getDateTime() {
return dateTime;
}
Resteasy 3.0.9 with this provider, (also has support for JAXB annotation - dependency on jackson-module-jaxb-annotations), with custom adapter (See above)
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson2-provider</artifactId>
<version>${resteasy.version}</version>
</dependency>
Both Resteasy and Jersey with this dependency (also did not work without custom config, same as last two - with adapter)
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.4.0</version>
</dependency>
We need to make sure to register the JacksonJaxbJsonProvider
So I guess it seems that any provider that uses Jackson, does not give you the deisred result, without some custom configuration, whether its through an adapter (as seen above) or some other custom configuration. The jersey-media-moxy
provider doesn't use Jackson.
For the most part, the information above is incorrect.
MOXy does not work by default. It works for serialization by simply calling toString()
, which may or may not be what you want, and it won't work when de-serializing. If you are using MOXy, until it supports Java8 time, you will need to use an XMLAdapter
Jackson you will need to configure its Java8 time support. This is the case with both Jersey and RESTEasy.
UPDATED: works with: <version>2.4.2</version>
When I try the above:
Caused by: java.lang.NoSuchMethodError: com.fasterxml.jackson.datatype.jsr310.ser.JSR310FormattedSerializerBase.findFormatOverrides(Lcom/fasterxml/jackson/databind/SerializerProvider;Lcom/fasterxml/jackson/databind/BeanProperty;Ljava/lang/Class;)Lcom/fasterxml/jackson/annotation/JsonFormat$Value;
I get the following error. Any ideas plse?
<jackson.version>2.8.0</jackson.version>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>${jackson.version}</version>
package com.jobs.spring.configuration;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JSR310Module;
@Provider
@Produces(MediaType.APPLICATION_JSON)
public class JacksonContextResolver implements ContextResolver<ObjectMapper> {
private static final ObjectMapper om = init();
@Override
public ObjectMapper getContext(Class<?> objectType) {
return om;
}
private static ObjectMapper init() {
ObjectMapper om = new ObjectMapper();
om.registerModule(new JSR310Module());
return om;
}
}
Also tried with the following as JSR310Module()
is deprecicated in 2.8.0
om.registerModule(new JavaTimeModule());
I don't remember the exact details of why I did this but I got it working with the following dependencies:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.4.2</version>
</dependency>
And the provider below:
@Provider
@Produces(MediaType.APPLICATION_JSON)
public class JacksonContextResolver implements ContextResolver<ObjectMapper> {
private static final ObjectMapper om = init();
@Override public ObjectMapper getContext(Class<?> objectType) {
return om;
}
private static ObjectMapper init() {
ObjectMapper om = new ObjectMapper();
om.registerModule(new JavaTimeModule());
return om;
}
}
Note that you can also play with the following flag:
om.configure(WRITE_DATES_AS_TIMESTAMPS, true/false);
(the actual class has more stuff in it but I believe it is irrelevant to this answer).