Java 8 date/time type not supported by default - when using RestEasy client POST request

34 views Asked by At

I am using RestEasy client POST request as in the following interface:

@POST
@Path("/somePath")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
Response getPoints(@QueryParam("assetId") String assetId, List<LocalDateTime> points);

Where 'points' is a list of Java 8 LocalDateTime.

I am using "jackson-datatype-jsr310" as a dependency.

I added as a provider custom Jackson config that implements ContextResolver and holds objectMapper And registered JavaTimeModule:

public class JacksonConfig implements ContextResolver<ObjectMapper> {
    private static final ObjectMapper objectMapper = new ObjectMapper();
....
        objectMapper.registerModule(new JavaTimeModule());

And Added the JacksonConfig to web.xml resteasy.providers.

My question is, when calling getPoints, I get the "Java 8 date/time type not supported by default " ERROR.

Do restEasy should use the JacksonConfig as a provider for serializing the POST object ? Because it seems it does not.

NOTE that when in another place, using restEasy for returning 'LocalDateTime' as an object , the serialization works fine after adding the "jackson-datatype-jsr310" and JavaTimeModule registeration (before that, it was the same error):

            LocalDateTime ldt = LocalDateTime.now();
            return Response.status(Response.Status.OK).entity(ldt).build();

what is the difference between the 2 serialization ? Do both should use the same objectMapper ?

Thanks,

Stacktrace of the error:

Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Java 8 date/time type `java.time.LocalDateTime` not supported by default: add Module "com.fasterxml.jackson.datatype:jackson-datatype-jsr310" to enable handling (through reference chain: java.util.LinkedList[0])
    at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:77) ~[jackson-databind-2.14.0-rc2.jar:2.14.0-rc2]
    at com.fasterxml.jackson.databind.SerializerProvider.reportBadDefinition(SerializerProvider.java:1306) ~[jackson-databind-2.14.0-rc2.jar:2.14.0-rc2]
    at com.fasterxml.jackson.databind.ser.impl.UnsupportedTypeSerializer.serialize(UnsupportedTypeSerializer.java:35) ~[jackson-databind-2.14.0-rc2.jar:2.14.0-rc2]
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContentsUsing(CollectionSerializer.java:171) ~[jackson-databind-2.14.0-rc2.jar:2.14.0-rc2]
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:116) ~[jackson-databind-2.14.0-rc2.jar:2.14.0-rc2]
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:107) ~[jackson-databind-2.14.0-rc2.jar:2.14.0-rc2]
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:25) ~[jackson-databind-2.14.0-rc2.jar:2.14.0-rc2]
    at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:480) ~[jackson-databind-2.14.0-rc2.jar:2.14.0-rc2]
    at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:400) ~[jackson-databind-2.14.0-rc2.jar:2.14.0-rc2]
    at com.fasterxml.jackson.databind.ObjectWriter$Prefetch.serialize(ObjectWriter.java:1568) ~[jackson-databind-2.14.0-rc2.jar:2.14.0-rc2]
    at com.fasterxml.jackson.databind.ObjectWriter.writeValue(ObjectWriter.java:1061) ~[jackson-databind-2.14.0-rc2.jar:2.14.0-rc2]
    at org.jboss.resteasy.plugins.providers.jackson.ResteasyJackson2Provider.writeTo(ResteasyJackson2Provider.java:345) ~[resteasy-jackson2-provider-4.7.7.Final.jar:4.7.7.Final]
    at org.jboss.resteasy.core.interception.jaxrs.AbstractWriterInterceptorContext.writeTo(AbstractWriterInterceptorContext.java:284) ~[resteasy-core-4.7.7.Final.jar:4.7.7.Final]
    at org.jboss.resteasy.core.interception.jaxrs.AbstractWriterInterceptorContext.syncProceed(AbstractWriterInterceptorContext.java:245) ~[resteasy-core-4.7.7.Final.jar:4.7.7.Final]
    at org.jboss.resteasy.core.interception.jaxrs.AbstractWriterInterceptorContext.proceed(AbstractWriterInterceptorContext.java:224) ~[resteasy-core-4.7.7.Final.jar:4.7.7.Final]
    at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.writeRequestBody(ClientInvocation.java:446) ~[resteasy-client-4.7.7.Final.jar:4.7.7.Final]
    at org.jboss.resteasy.client.jaxrs.engines.ManualClosingApacheHttpClient43Engine.writeRequestBodyToOutputStream(ManualClosingApacheHttpClient43Engine.java:625) ~[resteasy-client-4.7.7.Final.jar:4.7.7.Final]
    at org.jboss.resteasy.client.jaxrs.engines.ManualClosingApacheHttpClient43Engine.buildEntity(ManualClosingApacheHttpClient43Engine.java:584) ~[resteasy-client-4.7.7.Final.jar:4.7.7.Final]
    at org.jboss.resteasy.client.jaxrs.engines.ManualClosingApacheHttpClient43Engine.loadHttpMethod(ManualClosingApacheHttpClient43Engine.java:489) ~[resteasy-client-4.7.7.Final.jar:4.7.7.Final]
    at org.jboss.resteasy.client.jaxrs.engines.ManualClosingApacheHttpClient43Engine.invoke(ManualClosingApacheHttpClient43Engine.java:299) ~[resteasy-client-4.7.7.Final.jar:4.7.7.Final]
    ... 61 more

Expect the serializtion to work by using the objectMapper configured

1

There are 1 answers

1
Michael Gantman On

First of all if you were returning some instance of a class where one of the properties would be variable of type LocalDateTime, you would need to annotate that property with annotation similar to this:

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS")

However, if you just return your LocalDateTime as a stand-alone value you can format it in your code and return it as a String. Anyway good answer for the first option could be seen here: Spring Data JPA - ZonedDateTime format for json serialization