我正在使用JAX-RS 1.0学习Jackson。服务器返回HTTP响应,如下所示:
{
"url": "http://localhost:8080/api/123",
"created": "2018-05-26T09:21:26.301+02:00"
}
并且我需要将JSON响应反序列化为Java对象ProductCreated:
public class ProductCreated {
@JsonProperty("created")
private final ZonedDateTime created;
@JsonProperty("url")
private final String url;
public ProductCreated(String url) {
this.url = url;
this.created = ZonedDateTime.now();
}
@JsonCreator
public ProductCreated(
@JsonProperty("url") String url,
@JsonProperty("created") ZonedDateTime created) {
this.url = url;
this.created = created;
}
public String getUrl() {
return url;
}
public ZonedDateTime getCreated() {
return created;
}
}
在我的测试中,它无法构造ProductCreated对象:
Product p = new Product("123", "foo");
ProductCreated c = wr.type(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.post(ProductCreated.class, p);
客户端错误是:
无法构造java.time.ZonedDateTime(没有创建者,例如默认构造的实例)的实例:没有要从字符串值反序列化的字符串参数构造函数/工厂方法(‘2018-05-26T09:21:26.301
+ 02:00’)
在服务器端,我配置了JSON映射器(但是测试对客户端没有影响?):
private static JacksonJsonProvider newJacksonJsonProvider() {
ObjectMapper mapper =
new ObjectMap