json - 日期序列化忽略 REST 请求中的 ContextResolver
问题描述
我有一个基于 Wildfly 15 的应用程序,它使用 Yasson 序列化 REST 请求中的实体。我使用 javaee-api 8.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)
到 type 的字段java.util.Date
。但是,我更喜欢适用于所有日期字段的解决方案。不工作的解决方案是什么ContextResolver
?
请注意,WildflyContextResolver
在启动期间加载我的实现类(类加载断点),当我收到传入的 REST 请求时使用此解析器。
解决方案
由于您使用的是 JAX-RS 客户端,因此您需要向客户端注册提供程序。
Response response = target.path(REST_SERVICE_NAME)
.register(JsonbDateConfig.class)
.request()
.post(Entity.entity(dataTO, MediaType.APPLICATION_JSON));
推荐阅读
- counter - 计数器如何与异或门和 3 个输入一起工作
- android - 数据未显示在使用 RxJava 使用 Android Paging3 分页的列表中
- c# - 使用字符串包含特定数字
- javascript - 反转饼图上的滑块比例
- php - 如何访问 laravel 路由资源方法?
- java - 如果他们没有父母,那么只有具体的工厂是一个错误的决定吗?
- javascript - 客户端 Javascript 从后端快速服务器获取数据
- html - 如果在 ngOnInit 中使用订阅,*ngif 不起作用
- r - ggplot条形图与条形翻转方向
- mysql - 使用其他表中的数据更新第一行中的多个列而不连接