使用Jersey客户端,Java 8 Date API(Jsr310)出现问题
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了使用Jersey客户端,Java 8 Date API(Jsr310)出现问题,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2664字,纯文字阅读大概需要4分钟。
内容图文
![使用Jersey客户端,Java 8 Date API(Jsr310)出现问题](/upload/InfoBanner/zyjiaocheng/663/b689833f686f475788949084bf43b304.jpg)
我正在使用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)
内容总结
以上是互联网集市为您收集整理的使用Jersey客户端,Java 8 Date API(Jsr310)出现问题全部内容,希望文章能够帮你解决使用Jersey客户端,Java 8 Date API(Jsr310)出现问题所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。