You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JAX-RS客户端Jackson反序列化ZonedDateTime失败排查求助

解决JAX-RS 1.0客户端Jackson反序列化ZonedDateTime失败的问题

Great question! The root cause here is that your client-side Jackson ObjectMapper hasn't registered the JavaTimeModule—server-side configuration only applies to the server's serialization/deserialization pipeline, and the client uses a completely separate ObjectMapper instance that doesn't inherit those settings. JAX-RS 1.0 (and its reference implementation Jersey 1.x) doesn't include built-in support for Java 8's java.time types, so you need to explicitly configure the client to use a Jackson setup that handles these types.

核心解决方案:在客户端配置带JavaTimeModule的ObjectMapper

You'll need to create a custom ObjectMapper with JavaTimeModule registered, then tell your JAX-RS 1.0 client to use this mapper for processing JSON responses. Here's how to do it step by step:

1. 确保依赖包含jackson-datatype-jsr310

First, make sure your client project has the Jackson module for Java 8 time types. If using Maven, add this dependency:

<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jsr310</artifactId>
    <version>your-jackson-version</version>
</dependency>

(Replace your-jackson-version with the same version you're using on the server to avoid compatibility issues.)

2. 配置客户端使用自定义ObjectMapper

For JAX-RS 1.0 (Jersey 1.x), you'll register a JacksonJsonProvider with your pre-configured ObjectMapper when creating the client:

// Create and configure the ObjectMapper with JavaTimeModule
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule());
// Optional: Ensure ISO-8601 date strings are handled correctly (disable timestamp serialization)
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);

// Initialize JAX-RS 1.0 client
Client client = Client.create();
// Register the custom Jackson provider with our ObjectMapper
client.addProvider(new JacksonJsonProvider(objectMapper));

// Make your API call and deserialize to ProductCreated
WebResource webResource = client.resource("https://your-api-endpoint.com/products");
ProductCreated product = webResource.accept(MediaType.APPLICATION_JSON)
                                    .get(ProductCreated.class);

3. 验证ProductCreated类的字段映射

Double-check that your ProductCreated class's ZonedDateTime field is correctly mapped to the JSON created key. If your field name matches the JSON key exactly, no extra annotation is needed, but you can explicitly add @JsonProperty for clarity:

public class ProductCreated {
    private String url;
    @JsonProperty("created")
    private ZonedDateTime created;

    // Getters and setters
}

关键注意事项

  • Server vs Client Separation: The server's ObjectMapper configuration has no impact on the client. They are separate instances running in different contexts, so you must replicate the time module registration on the client side.
  • JAX-RS 1.0 vs 2.0: Don't confuse JAX-RS 1.0 API with 2.0. JAX-RS 1.0 uses Client.create() instead of ClientBuilder.newClient(), and provider registration works differently.
  • ISO Format Compatibility: JavaTimeModule natively handles ISO-8601 formatted date strings (like 2024-05-20T14:30:00Z or 2024-05-20T14:30:00+02:00), so as long as the server returns valid ISO strings, the module will parse them correctly into ZonedDateTime.

内容的提问来源于stack exchange,提问作者Mincong Huang

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 06:56:36