如何在Spring MVC中使用Jackson和Jersey 2 Client对Joda DateTime进行反序列化?

时间:2014-08-20 12:27:24

标签: spring-mvc jersey jackson jodatime spring-boot

我一直在用这个概念证据抨击我的头脑。我想使用一个REST端点,该端点返回带有ISO8601 UTC时间戳的JSON有效负载:

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

我希望使用命令行Java客户端来使用它,该客户端使用Jackson / Spring Boot编写为Jersey 2客户端。编组POJO定义如下:

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

  ...
}

遵循以下建议:

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

并使用以下Gradle依赖项:

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

我总是收到这个错误:

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)

我放弃了。我究竟做错了什么?

客户端配置如下:

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");
  }

  ...  

}

注册的提供者被定义为(与客户端在同一个包中):

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

}

我尝试使用/不注册提供程序并注释该字段以使用DateTimeDeserializer(通过@JsonDeserialize) - 只是为了得到错误,因为&#34;没有默认的no-arg构造函数可用&#34;。

如果我使用标准java.util.Date而不是

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

它像微风一样。

任何线索?谢谢你的帮助。

5 个答案:

答案 0 :(得分:12)

以下测试工作正常:

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配置:

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

答案 1 :(得分:6)

对于其他任何有问题的人来说,其他答案都没有完全完整,所以这个

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

JacksonJaxbJsonProvider provider = new JacksonJaxbJsonProvider();

provider.setMapper(new JodaMapper());

target.register(provider);

答案 2 :(得分:4)

我遇到了同样的问题。你要做的是在泽西岛注册自定义ObjectMapper:

@Component
public class JerseyConfig extends ResourceConfig {

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

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

答案 3 :(得分:1)

Piotor Glazar以一种方式工作而不是另一种方式。 (它会将日期写为时间戳,而不是ISO8601 UTC。)

他解决方案的一个变化是禁用SerializationFeature.WRITE_DATES_AS_TIMESTAMPS。

这是适合我的解决方案。

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

答案 4 :(得分:0)

如果你想在Jersy客户端的J2EE项目中实现相同的目的,请参考下面的代码。

    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);

日期DeSerialiser在下面,

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

希望这会对你有所帮助。