使用Jersey客户端时,Java 8 Date API(Jsr310)存在问题

使用Jersey客户端时,Java 8 Date API(Jsr310)存在问题,java,rest,jersey,jackson,jsr310,Java,Rest,Jersey,Jackson,Jsr310,我正在使用jersey客户端执行一个小PoC来使用REST服务,并且我遇到了LocalDateTime格式的字段问题。 REST服务响应如下所示: { "id": 12, "infoText": "Info 1234", "creationDateTime": "2001-12-12T13:40:30" } ... Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not instan

我正在使用jersey客户端执行一个小PoC来使用REST服务,并且我遇到了LocalDateTime格式的字段问题。 REST服务响应如下所示:

{
    "id": 12,
    "infoText": "Info 1234",
    "creationDateTime": "2001-12-12T13:40:30"
}
...
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not instantiate value of type [simple type, class java.time.LocalDateTime] from String value ('2001-12-12T13:40:30'); no single-String constructor/factory method
 at [Source: org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream@21b2e768; line: 1, column: 32] (through reference chain: com.my.poc.jerseyclient.Info["creationDateTime"])
...
和我的实体类:

package com.my.poc.jerseyclient;

import java.time.LocalDateTime;

public class Info {

    private Long id;
    private String infoText;
    private LocalDateTime creationDateTime;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getInfoText() {
        return infoText;
    }

    public void setInfoText(String infoText) {
        this.infoText = infoText;
    }

    public LocalDateTime getCreationDateTime() {
        return creationDateTime;
    }

    public void setCreationDateTime(LocalDateTime creationDateTime) {
        this.creationDateTime = creationDateTime;
    }

}
my pom.xml中的依赖项:

<dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-client</artifactId>
            <version>2.21</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.jaxrs</groupId>
            <artifactId>jackson-jaxrs-json-provider</artifactId>
            <version>2.6.1</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-jsr310</artifactId>
            <version>2.6.1</version>
        </dependency>
    </dependencies>
我得到了这样一个例外:

{
    "id": 12,
    "infoText": "Info 1234",
    "creationDateTime": "2001-12-12T13:40:30"
}
...
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not instantiate value of type [simple type, class java.time.LocalDateTime] from String value ('2001-12-12T13:40:30'); no single-String constructor/factory method
 at [Source: org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream@21b2e768; line: 1, column: 32] (through reference chain: com.my.poc.jerseyclient.Info["creationDateTime"])
...

我错过了什么?我在RestTemplate(SpringRESTClient)上尝试了这一点,它在没有任何配置的情况下工作得非常好。

jsr310模块仍然需要向Jackson注册。您可以在as中执行此操作,在as中,您可以使用
ObjectMapper

ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JSR310Module());
然后向客户端注册
ContextResolver
。将会发生的是,
JacksonJsonProvider
应该调用
getContext
方法,并检索要用于(反)序列化的
ObjectMapper

您也可以在服务器端使用相同的
ContextResolver
。如果您只在客户端上需要此功能,另一种方法是使用
ObjectMapper
简单地构造
JacksonJsonProvider
。简单一点

new JacksonJsonProvider(mapper)

将所有功能组合在一起以实现客户端功能:

ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JSR310Module());

JacksonJsonProvider provider = new JacksonJsonProvider(mapper);
Client client = ClientBuilder.newClient(new ClientConfig().register(provider));

它很管用,谢谢!我想我必须以某种方式向客户注册,但我不知道如何注册。感谢您如此清晰明了地给出答案。请注意,自Jackson 2.6.0以来,
JSR310模块
被弃用,取而代之的是
JavaTimeModule