我尝试使用 Resteasy CLIENT 调用一个 REST 服务
在我的服务中,我使用 springboot 创建并返回一个 LocalDateTime 属性
如果我使用这种依赖
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.8.8</version>
</dependency>
这是我的 bean 中的代码:
private LocalDateTime dtPublicacao;
这是我调用我的服务时的结果:
dtPublicacao: "2017-04-20T00:00:00"
如果我删除这个结果:
dtPublicacao: {
hour: 0,
minute: 0,
second: 0,
nano: 0,
dayOfYear: 110,
dayOfWeek: "THURSDAY",
month: "APRIL",
dayOfMonth: 20,
year: 2017,
monthValue: 4,
chronology: {
id: "ISO",
calendarType: "iso8601"
}
}
因此在我的客户端中我创建了相同的模型并使用它来执行 Get
Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://localhost:8585").path("/edital/");
try{
List<EditalVO> response = target.request().get(new GenericType<List<EditalVO>>(){});
return response;
}catch (NotFoundException e) {
throw new NotFoundException();
}
因此,如果我将 LocalDateTime 放入我的客户端,我会收到此错误:
Caused by: org.codehaus.jackson.map.JsonMappingException: Can not instantiate value of type [simple type, class java.time.LocalDateTime] from JSON String; no single-String constructor/factory method (through reference chain: br.com.lumera.protesto.edital.vo.EditalVO["dtExpiracao"])
为了解决我需要在我的客户端表单中将 LocalDateTime 更改为 Date 并添加
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date dtPublicacao;
我已经尝试在我的客户端中添加相同的依赖项,但他注册了 LocalDateTimeSerializer.class,但是当我调用 REST 时他没有通过序列化方法,我又遇到了错误。
我可以从我的 Restasey 客户端接收和发送 LocalDateTime 吗??
谢谢
更新 我也已经尝试这样做了:
ObjectMapper obj = new ObjectMapper();
obj.registerModule(new JavaTimeModule())
没有成功,然后我尝试在我的客户中:
client.register(obj);
没有成功...如果我调试我的应用程序,他会进入方法
public LocalDateTimeSerializer(DateTimeFormatter f) {
super(LocalDateTime.class, f);
}
private LocalDateTimeSerializer(LocalDateTimeSerializer base, Boolean useTimestamp, DateTimeFormatter f) {
super(base, useTimestamp, f);
}
在 LocalDateTimeSerializer 中但不进入序列化方法或反序列化
最佳答案
关于java-8 - 使用 LocalDateTime 的客户端 Resteasy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43621355/