日期序列化忽略REST请求中的ContextResolver



我有一个基于Wildfly 15的应用程序,它使用Yasson序列化REST请求中的实体。我使用javaeeapi8.0.1并创建了一个ContextResolver来配置日期序列化格式,如https://stackoverflow.com/a/56300246/584532.

@Provider
@Produces(MediaType.APPLICATION_JSON)
public class JsonbDateConfig implements ContextResolver<Jsonb> {
// ...
}

但是,当使用以下代码发送REST请求时,会忽略配置(JsonbDateConfig的方法中不会触发调试断点(。

Response response = target.path(REST_SERVICE_NAME)
.request()
.post(Entity.entity(dataTO, MediaType.APPLICATION_JSON));

因此,生成的JSON包含一个格式不正确的日期值。

我可以创建一个JsonbAdapter,并将注释@JsonbTypeAdapter(DateAdapter.class)添加到类型为java.util.Date的字段中。但是,我更喜欢适用于所有日期字段的解决方案。ContextResolver不工作的解决方案是什么?

请注意,Wildfly在启动期间(类加载断点(加载ContextResolver的实现类,并且在接收传入的REST请求时使用此解析器。

由于您使用的是JAX-RS客户端,因此需要向客户端注册提供程序。

Response response = target.path(REST_SERVICE_NAME)
.register(JsonbDateConfig.class)
.request()
.post(Entity.entity(dataTO, MediaType.APPLICATION_JSON));

最新更新