如何重用 Jersey 的 JSON/JAXB 进行序列化?
我有一个使用 Jersey 实现的 JAX-RS REST 服务。 JAX-RS/Jersey 的一个很酷的功能是,只需添加一些 Java 注释即可轻松地将 POJO 转换为 REST 服务...包括使用 JAXB 注释将 POJO 转换为 JSON 的简单机制。
现在,我希望能够利用这个很酷的 JSON 化功能来实现非 REST 目的 - 我希望能够将其中一些对象作为 JSON 文本序列化到磁盘。 下面是我想要序列化的一个示例 JAXB 对象:
@XmlRootElement(name = "user")
public class UserInfoImpl implements UserInfo {
public UserInfoImpl() {}
public UserInfoImpl(String user, String details) {
this.user = user;
this.details = details;
}
public String getUser() { return user; }
public void setUser(String user) { this.user = user; }
public String getDetails() { return details; }
public void setDetails(String details) { this.details = details; }
private String user;
private String details;
}
Jersey 可以将其中之一转换为 json,无需附加信息。 我想知道 Jersey 是否在 API 中公开了此功能以满足像我这样的需求? 到目前为止我还没有找到它......
谢谢!
更新 2009-07-09:我了解到我可以使用 Providers 对象几乎做我想要的事情:
@Context Providers ps;
MessageBodyWriter uw = ps.getMessageBodyWriter(UserInfoImpl.class, UserInfoImpl.class, new Annotation[0], MediaType.APPLICATION_JSON_TYPE);
uw.writeTo(....)
...这会将对象作为 json 写入任何输出流,这对我来说是完美的,但我只能使用 @Component 对象中的 @Context 获取 Providers 对象。 有谁知道如何从常规的、未注释的 POJO 访问它? 谢谢!
I have a JAX-RS REST service implemented using Jersey. One of the cool features of JAX-RS/Jersey is how easily a POJO can be turned into a REST service, simply by sprinkling a few Java annotations... including a trivially easy mechanism for translating POJOs to JSON - using JAXB annotations.
Now, I'd like to be able to take advantage of this cool JSON-ifying functionality for non-REST purposes - I'd love to be able to just serialize some of these objects to disk, as JSON text. Here's an example JAXB object that I'd want to serialize:
@XmlRootElement(name = "user")
public class UserInfoImpl implements UserInfo {
public UserInfoImpl() {}
public UserInfoImpl(String user, String details) {
this.user = user;
this.details = details;
}
public String getUser() { return user; }
public void setUser(String user) { this.user = user; }
public String getDetails() { return details; }
public void setDetails(String details) { this.details = details; }
private String user;
private String details;
}
Jersey can turn one of these into json with no additional info. I'm wondering if Jersey has exposed this functionality in the API for needs like mine? I've had no luck finding it so far...
Thanks!
UPDATE 2009-07-09: I have learned that I can use the Providers object to almost do exactly what I want:
@Context Providers ps;
MessageBodyWriter uw = ps.getMessageBodyWriter(UserInfoImpl.class, UserInfoImpl.class, new Annotation[0], MediaType.APPLICATION_JSON_TYPE);
uw.writeTo(....)
... This writes the object as json to any outputstream, which would be perfect for me, but I can only get at the Providers object using @Context from a @Component object. Does anyone know how to access it from a regular, un-annotated POJO? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
Jersey 使用几个不同的框架,具体取决于您是否使用mapped()、badgerfish() 或natural() 表示法。 自然通常是人们想要的。 我相信,这是使用非常好的(并且非常快)的独立 Jackson JSON 处理器实现的,它来自 Object->JAXB->JSON。 然而 Jackson 还提供了它自己的 JAX-RS 提供程序来直接使用 Object->JSON。
事实上,他们甚至添加了对 JAXB 注释的支持。 看看
http://wiki.fasterxml.com/JacksonJAXBAnnotations
我认为这最终就是您想要的为了。 Jackson 进行 Object<->JSON 处理...Jersey 只是为您打电话
Jersey uses a couple different frameworks depending on whether you use mapped(), badgerfish(), or natural() notation. Natural is usually the one people want. And that's implemented using the very good (and very fast) standalone Jackson JSON processor, I believe, which goes from Object->JAXB->JSON. However Jackson also provides it's own JAX-RS provider to go direct Object->JSON.
In fact, they even added support for JAXB annotations. Have a look at
http://wiki.fasterxml.com/JacksonJAXBAnnotations
I think that's ultimately what you are looking for. Jackson does Object<->JSON processing...Jersey just makes the calls for you
以下是使用 JAXB 将对象映射到 JSON(使用 Jackson)的简单示例:
http://ondra.zizka.cz/stranky/programovani/java/jaxb-json-jackson-howto.texy
Here's a simple brief example of using JAXB to map objects to JSON (using Jackson):
http://ondra.zizka.cz/stranky/programovani/java/jaxb-json-jackson-howto.texy
JAXB 注释在序列化为 XML 时工作正常。
主要问题是 JAXB 不支持空数组。 因此,当通过 jaxb 注释将这样的内容序列化为
json 时,所有空数组都会变为 null 而不是 []。
要解决这个问题,你可以通过 jackson 将 pojo 直接序列化为 json。
看看 Jersey 的用户指南:
http://jersey.java.net/nonav/documentation/latest /user-guide.html#d0e1959
这是在没有 JAXB 的情况下使用 Jackson 提供程序的最佳方式。 此外,您始终可以通过从其网站下载 jackson-all-xyz-jar 来使用最新版本的 jackson。
此方法不会干扰您的 jaxb 注释,因此我建议尝试一下!
JAXB annotations work fine when serializing to XML.
The main problem is that JAXB does not support empty arrays. So when serializing something like this...
...to json via jaxb anottations all your empty arrays become null instead of [].
To solve this you can just serialize your pojos directly to json via jackson.
Take a look at this from Jersey's user guide:
http://jersey.java.net/nonav/documentation/latest/user-guide.html#d0e1959
This is the best way to use Jackson provider without JAXB. Moreover, you can always use the latest version of jackson by downlaoding jackson-all-x.y.z-jar from its web.
This method will not interfere with your jaxb annotations so I would suggest to have a try!
由于 Jersey 是 JAX-RS 的参考实现,并且 JAX-RS 完全专注于提供实现 REST 服务端点的标准方法,因此序列化有效负载的问题留给其他标准。
我认为,如果他们在 JAX-RS 标准中包含对象序列化,它将很快成为一个巨大的多头野兽,难以实现并失去一些重点。
我很欣赏 Jersey 专注于提供干净且易于使用的 REST 端点。 就我而言,我刚刚对包含所有 JAXB 管道的父级进行了子类化,因此在二进制和 XML 之间编组对象非常干净。
Since Jersey is a reference implementation of JAX-RS and JAX-RS is focused completely on providing a standard way of implementing the end-point for the REST service the issues of serializing the payload is left to other standards.
I think that if they included object serialization in the JAX-RS standard it would quickly become a large multi-headed beast that would be difficult to implement and loose some of it's focus.
I appreciate how focused Jersey is on delivering clean and simple to use REST endpoints. In my case I've just subclassed a parent that has all the JAXB plumbing in it so marshalling objects between binary and XML is very clean.
通过一些 Jersey 特定的引导,您可以使用它为您创建必要的 JSON 对象。 您需要包含以下依赖项(您可以使用bundle,但如果您使用Weld 进行测试,则会导致问题):
从那里您可以创建一个JAXB 带注释的类。 下面是一个示例:
然后您可以创建以下单元测试:
这种方法的优点是它将所有内容保留在 JEE6 API 中,除了测试和获取提供程序之外,不需要明确需要任何外部库。 但是,您需要创建 MultivaluedMap 的实现,因为标准中没有提供任何内容,而且我们实际上并不使用它。 它可能也比GSON慢,并且比必要的复杂得多。
With a little Jersey specific bootstrapping, you can use it to create the necessary JSON objects for you. You need to include the following dependencies (you can use bundle, but it will cause problems if you are using Weld for testing):
From there you can create a JAXB annotated class. The following is an example:
Then you can create the following unit test:
The advantage to this approach is it keeps everything within the JEE6 API, no external libraries are explicitly needed except for testing and getting the providers. However, you need to create an implementation of MultivaluedMap since there is nothing provided in the standard and we don't actually use it. It may also be slower than GSON, and a lot more complicated than necessary.
我理解 XML 视图,但要求 POJO 支持 JSON 作为标准设备会显示出一些远见。 如果您的实现是 JSON 并且您的客户端是 JavaScript RIA,则必须用特殊字符修改 JSON 标识符是没有意义的。
另外,Java Bean 并不是 POJO。 我想在我的 Web 层的外表面使用类似的东西:
没有默认构造函数,没有 getter/setter 噪音,只有一个带有我自己的注释的 POJO。
I understand XML views but it would have shown some foresight to require JSON support for POJOs as standard equipment. Having to doctor up JSON identifiers with special characters makes no sense if your implementation is JSON and your client is a JavaScript RIA.
Also, not that Java Beans are NOT POJOs. I would like to use something like this on the outer surface of my web tier:
No default constructor, no getter/setter noise, just a POJO with my own annotations.