编程语言
首页 > 编程语言> > 使用Jersey客户端,Java 8 Date API(Jsr310)出现问题

使用Jersey客户端,Java 8 Date API(Jsr310)出现问题

作者:互联网

我正在使用jersey客户端进行一点PoC,以使用REST服务,并且使用LocalDateTime格式的字段遇到问题.
REST服务响应如下:

{
    "id": 12,
    "infoText": "Info 1234",
    "creationDateTime": "2001-12-12T13:40:30"
}

和我的实体类:

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

}

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

我的代码使用jersey客户端执行GET / api / info / 123:

Client client = ClientBuilder.newClient(new ClientConfig().register(JacksonJsonProvider.class);
WebTarget webTarget = client.target("http://localhost:8080/api/").path("info/");
Response response = webTarget.path("123").request(MediaType.APPLICATION_JSON_TYPE).get();
System.out.println("GET Body (object): " + response.readEntity(Info.class));

我得到这样的异常:

...
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(Spring rest client)尝试了一下,它在没有任何配置的情况下可以完美地工作.

解决方法:

jsr310模块仍然需要向Jackson进行注册.您可以在ContextResolver中将其设置为seen here,在其中向ObjectMapper注册Jsr310Module

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

然后,您向客户端注册ContextResolver.将会发生的事情是JacksonJsonProvider应该调用getContext方法,并检索要用于(反序列化)的ObjectMapper.

您还可以在服务器端使用相同的ContextResolver.如果仅在客户端上需要此功能,则另一种方法是使用ObjectMapper轻松构造JacksonJsonProvider.更简单一点

new JacksonJsonProvider(mapper)

标签:rest,jersey,jackson,jsr310,java
来源: https://codeday.me/bug/20191119/2038630.html