我尝试使用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:&#34; 2017-04-20T00:00:00&#34;
如果我删除了结果:
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:不能 实例化类型[simple type,class java.time.LocalDateTime]的值 来自JSON String;没有单字符串构造函数/工厂方法(通过 参考链: br.com.lumera.protesto.edital.vo.EditalVO [&#34; dtExpiracao&#34;])
要解决我需要将我的客户端表单LocalDateTime更改为Date并添加
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date dtPublicacao;
我已经尝试在我的客户端中添加相同的依赖项,但是他注册了LocalDateTimeSerializer.class,但是当我调用REST时,他没有通过serialize方法,我又得到了错误。
我可以从Restasey CLIENT接收和发送LocalDateTime吗?
TKS
更新 我已经尝试过这样做了:
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中但不要输入序列化方法或反序列化
答案 0 :(得分:1)
答案 1 :(得分:0)
如何将ObjectMapper
配置到resteasy-client模块中?你可以看到这个question。
我已经写了两个测试,一方如何在github上安排另一方。
如果您希望客户端适合服务器端,则必须将模式DateFormat设置为"yyyy-MM-dd'T'HH:mm:ss"
,例如:
ObjectMapper#setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"))
或您可以将@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);