javax.ws.rs.client.Entity 如何将对象序列化为 json?
How is javax.ws.rs.client.Entity serializing objects to json?
Entity.json(T entity)
对 serialize/deserialize 对象使用什么序列化程序?是否可以使用自定义序列化程序?
在我的例子中,序列化是错误的,因为我的对象包含具有 Guava Optional 数据类型的字段,并且缺少的值返回为 {"present":false}
而不是 null
。
JAX-RS 未指定 JSON 序列化程序,它取决于您的配置。例如,Jersey JAX-RS 允许多个 (https://jersey.java.net/documentation/latest/media.html),包括
- MOXy
- Java API 用于 JSON 处理 (JSON-P)
- 杰克逊
- 抛弃
但更好的解决方案是不对字段使用 Optional
(Guava 或 Java 8)。参见 http://blog.joda.org/2014/11/optional-in-java-se-8.html
My only fear is that Optional will be overused. Please focus on using
it as a return type (from methods that perform some useful piece of
functionality) Please don't use it as the field of a Java-Bean.
没有直接解决你的问题。我建议您使用 Googles Gson 作为解析器。它非常灵活且可配置。
它还会跳过空白字段,因此 json 大小不会太大。
Entity.json(T entity)
对 serialize/deserialize 对象使用什么序列化程序?是否可以使用自定义序列化程序?
在我的例子中,序列化是错误的,因为我的对象包含具有 Guava Optional 数据类型的字段,并且缺少的值返回为 {"present":false}
而不是 null
。
JAX-RS 未指定 JSON 序列化程序,它取决于您的配置。例如,Jersey JAX-RS 允许多个 (https://jersey.java.net/documentation/latest/media.html),包括
- MOXy
- Java API 用于 JSON 处理 (JSON-P)
- 杰克逊
- 抛弃
但更好的解决方案是不对字段使用 Optional
(Guava 或 Java 8)。参见 http://blog.joda.org/2014/11/optional-in-java-se-8.html
My only fear is that Optional will be overused. Please focus on using it as a return type (from methods that perform some useful piece of functionality) Please don't use it as the field of a Java-Bean.
没有直接解决你的问题。我建议您使用 Googles Gson 作为解析器。它非常灵活且可配置。
它还会跳过空白字段,因此 json 大小不会太大。