我尝试使用 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 输入客户端,则会收到此错误:
导致:org.codehaus.jackson.map.JsonMappingException:不能 实例化类型 [简单类型,类 java.time.LocalDateTime] 的值 来自 JSON 字符串;没有单字符串构造函数/工厂方法(通过 参考链: 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 中,但不要进入序列化方法或反序列化
如何将
ObjectMapper
配置到resteasy-client模块中?你可以看到这个问题。
我在 github 上编写了两个测试如何一侧适合另一侧。
如果希望客户端适应服务器端,则必须将模式 DateFormat 设置为
"yyyy-MM-dd'T'HH:mm:ss"
,例如:
ObjectMapper#setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"))
OR 您可以将
@JsonFormat
用作:
@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss")
private Date dtPublicacao;
如果希望服务器端适合客户端,则必须将模式 DateTimeFormatter 设置为
"yyyy-MM-dd HH:mm:ss"
,例如:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
Module module = new JavaTimeModule().addDeserializer(LocalDateTime.class
,new LocalDateTimeDeserializer(formatter));
ObjectMapper#registerModule(module);
对于环境“java se app + Resteasy-client 6”(不是spring,不是wildfly/jboss)只有这个有帮助,无数其他方法没有:
pom 中常见的依赖
com.fasterxml.jackson.datatype 杰克逊数据类型-jsr310 2.16.1 /依赖项>添加新类ObjectMapperProvider
@提供商 公共类 ObjectMapperProvider 实现 ContextResolver { 静态最终 JsonMapper MAPPER = JsonMapper.builder() .findAndAddModules() .build();
@覆盖 公共 ObjectMapper getContext(最终 Class 类型) { 返回映射器; } }
严格在目标端而不是客户端注册ObjectMapperProvider。
ResteasyClient 客户端 = (ResteasyClient)ClientBuilder.newBuilder().newClient();
SomeAPI api = 客户端 .target(UriBuilder.fromPath("https://api.api.cc/")) .register(new ObjectMapperProvider()) .proxy(SomeAPI.class);