How to deserialize Joda DateTime using Jackson wit

2020-06-30 16:52发布

问题:

I've been bashing my head with this proof of concept for a while. I want to consume a REST endpoint that returns JSON payload with an ISO8601 UTC timestamp:

{  ...
  "timestamp" : "2014-08-20T11:51:31.233Z" 
}

and I want to consume it using a command line Java client written as a Jersey 2 client with Jackson/Spring Boot. The marshalling POJO is defined as follows:

@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(Include.NON_NULL)
public class GreetingResource
{
  @JsonProperty("timestamp")
  private DateTime date;

  ...
}

After following recommendations shown in:

https://jersey.java.net/documentation/latest/user-guide.html#json.jackson

and using the following Gradle dependencies:

dependencies {
    compile("org.springframework.boot:spring-boot-starter")
    compile("org.springframework.boot:spring-boot-starter-logging")
    compile("joda-time:joda-time")
    compile("com.fasterxml.jackson.core:jackson-core")
    compile("com.fasterxml.jackson.core:jackson-annotations")
    compile("com.fasterxml.jackson.core:jackson-databind")
    compile("com.fasterxml.jackson.datatype:jackson-datatype-joda")
    compile("com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:2.3.3")
    compile("org.apache.commons:commons-lang3:3.3.2")
    compile("org.glassfish.jersey.core:jersey-client:2.2")
    compile("org.glassfish.jersey.media:jersey-media-json-jackson:2.2")
    testCompile("org.springframework.boot:spring-boot-starter-test")
}

I always get this error:

Exception in thread "main" javax.ws.rs.ProcessingException: Error reading entity from input stream.
  at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:849)
  at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:768)
  at org.glassfish.jersey.client.InboundJaxrsResponse.readEntity(InboundJaxrsResponse.java:96)
  at org.glassfish.jersey.client.ScopedJaxrsResponse.access$001(ScopedJaxrsResponse.java:56)
  at org.glassfish.jersey.client.ScopedJaxrsResponse$1.call(ScopedJaxrsResponse.java:77)
  at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
  at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
  at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
  at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:397)
  at org.glassfish.jersey.client.ScopedJaxrsResponse.readEntity(ScopedJaxrsResponse.java:74)
  at client.GreetingClient.processResponse(GreetingClient.java:62)
  at client.GreetingClient.performGet(GreetingClient.java:53)
  at client.GreetingService.internalLoadGreeting(GreetingService.java:44)
  at client.GreetingService.LoadGreeting(GreetingService.java:27)
  at client.Application.main(Application.java:25)
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not instantiate value of type [simple type, class org.joda.time.DateTime] from String value ('2014-08-20T12:19:36.358Z'); no single-String constructor/factory method (through reference chain: client.GreetingResource["timestamp"])
  at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator._createFromStringFallbacks(StdValueInstantiator.java:428)
  at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.createFromString(StdValueInstantiator.java:299)
  at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromString(BeanDeserializerBase.java:1150)
  at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:139)
  at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:126)
  at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:525)
  at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:99)
  at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:242)
  at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:118)
  at com.fasterxml.jackson.databind.ObjectReader._bind(ObjectReader.java:1233)
  at com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:677)
  at com.fasterxml.jackson.jaxrs.base.ProviderBase.readFrom(ProviderBase.java:777)
  at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.aroundReadFrom(ReaderInterceptorExecutor.java:188)
  at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:134)

I have given up. What am I doing wrong?

The client is configured as follows:

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Invocation.Builder;
import javax.ws.rs.client.WebTarget;
import org.glassfish.jersey.client.ClientConfig;

...

@Component
public class GreetingClient
{

  private final WebTarget serviceWebTarget;

  {
    ClientConfig config = new ClientConfig();
    config.register(MyObjectMapperProvider.class);
    config.register(JacksonFeatures.class);
    Client client = ClientBuilder.newClient(config);
    this.serviceWebTarget = client.target("http://myserver:8080");
  }

  ...  

}

The registered provider is defined as (in the same package as the client):

@Provider
public class MyObjectMapperProvider implements ContextResolver<ObjectMapper>
{
  private final static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");


  @Override
  public ObjectMapper getContext(Class<?> type)
  {
    final ObjectMapper result = new JodaMapper();
    result.setDateFormat(dateFormat);
    return result;
  }

}

I have tried with/without registering the provider and also annotating the field to use DateTimeDeserializer (through @JsonDeserialize) - only to get an error because "there is no default no-arg constructor available".

If I use standard java.util.Date instead with

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

it works like a breeze.

Any clues? Thanks for your help.

回答1:

Below test works OK:

import java.io.IOException;

import org.joda.time.DateTime;
import org.junit.Test;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.joda.JodaModule;

public class JacksonTest {

    private static final String json = "{ \n" +
            "  \"timestamp\" : \"2014-08-20T11:51:31.233Z\" \n" +
            "}";

    @Test
    public void test() throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.registerModule(new JodaModule());

        System.out.println(mapper.readValue(json, GreetingResource.class));
    }
}

@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(Include.NON_NULL)
class GreetingResource {
    @JsonProperty("timestamp")
    private DateTime date;

    public DateTime getDate() {
        return date;
    }

    public void setDate(DateTime date) {
        this.date = date;
    }

    @Override
    public String toString() {
        return "GreetingResource{" +
                "date=" + date +
                '}';
    }
}

Maven configuration:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.4.1.1</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.4.1.3</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.4.1</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-joda</artifactId>
    <version>2.4.0</version>
</dependency>
<dependency>
    <groupId>joda-time</groupId>
    <artifactId>joda-time</artifactId>
    <version>2.4</version>
</dependency>


回答2:

For anyone else having issues, none of the other answers were really fully complete so its this

WebTarget target = ClientBuilder.newClient().target(host);

JacksonJaxbJsonProvider provider = new JacksonJaxbJsonProvider();

provider.setMapper(new JodaMapper());

target.register(provider);


回答3:

I have come across the same problem. What you have to do is to register custom ObjectMapper in Jersey:

@Component
public class JerseyConfig extends ResourceConfig {

    public JerseyConfig() {
        JacksonJaxbJsonProvider provider = new JacksonJaxbJsonProvider();
        provider.setMapper(new JodaMapper());

        register(provider);
        packages("endpoints.package.names");
    }
}


回答4:

Piotor Glazar works one way but not the other. (It will write the date as a timestamp, rather than ISO8601 UTC.)

The one change from his solution is to disable SerializationFeature.WRITE_DATES_AS_TIMESTAMPS.

Here is the solution that worked for me.

public class JerseyConfig extends ResourceConfig {

    public JerseyConfig() {
        JacksonJaxbJsonProvider provider = new JacksonJaxbJsonProvider();
        JodaMapper jodaMapper = new JodaMapper();
        jodaMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        provider.setMapper(jodaMapper);

        register(provider);
    }
}


回答5:

If you want to achieve the same in J2EE project for Jersy client, please refer the code below.

    final Client client = ClientBuilder.newClient();
    final WebTarget target = client.target(URI);

    final JacksonJaxbJsonProvider provider = new JacksonJaxbJsonProvider();
    final JodaMapper jodaMapper = new JodaMapper();
    jodaMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);

    //while sending date to server.
    //Use a consistent format to send to server
    final DateFormat format = new SimpleDateFormat(DATE_TIME_FORMAT);
    jodaMapper.setDateFormat(format);

    //receiving date from server. [Deserialize]
    //expect any ISO format or any other format and make sure we handle it.
    final SimpleModule module = new SimpleModule();
    module.addDeserializer(Date.class, new CustumDateDeserializer());
    jodaMapper.registerModule(module);

    provider.setMapper(jodaMapper);

    target.register(provider);

And the Date DeSerialiser is below,

private class CustumDateDeserializer extends JsonDeserializer<Date> {

    @Override
    public Date deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {

        final String stringDate = jp.getText();
        Date parsedDate = null;

        if(stringDate == null || stringDate.trim().isEmpty()) {
            return parsedDate;
        }

        try{
            final DateTimeFormatter parser = ISODateTimeFormat.dateTime();
            parsedDate = parser.parseDateTime(stringDate).toDate();
        }catch(IllegalArgumentException e) {
            //use the default parser if the given date is not iso-format.
            parsedDate = new DateDeserializers.DateDeserializer().deserialize(jp, ctxt);
        }

        return parsedDate;
    }
}

Hope this will help you.