如何重用 Jersey 的 JSON/JAXB 进行序列化?序列化、Jersey、JSON、JAXB

2023-09-06 15:00:34 作者:像风很洒脱

我有一个使用 Jersey 实现的 JAX-RS REST 服务.JAX-RS/Jersey 的一个很酷的特性是 POJO 可以很容易地转换为 REST 服务,只需添加一些 Java 注释......包括一种将 POJO 转换为 JSON 的简单机制 - 使用 JAXB 注释.

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.

现在,我希望能够将这种酷炫的 JSON 化功能用于非 REST 目的 - 我希望能够将其中一些对象序列化到磁盘,作为 JSON 文本.这是我想要序列化的示例 JAXB 对象:

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 可以将其中之一转换为 json,无需额外信息.我想知道 Jersey 是否已经在 API 中公开了此功能以满足我的需求?到目前为止我还没有找到它......

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...

谢谢!

UPDATE 2009-07-09:我了解到我可以使用 Providers 对象来几乎做我想做的事:

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(....)

... 这会将对象作为 json 写入任何输出流,这对我来说是完美的,但我只能使用 @Component 对象中的 @Context 获取 Providers 对象.有谁知道如何从常规的、未注释的 POJO 访问它?谢谢!

... 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!

推荐答案

Jersey 使用几个不同的框架,具体取决于您使用的是 mapped()、badgerfish() 还是 natural() 表示法.自然通常是人们想要的.我相信,这是使用非常好(而且非常快)的独立 Jackson JSON 处理器实现的,它来自 Object->JAXB->JSON.然而,Jackson 也提供了它自己的 JAX-RS 提供程序来直接使用 Object->JSON.

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.

事实上,他们甚至添加了对 JAXB 注释的支持.来看看

In fact, they even added support for JAXB annotations. Have a look at

http://wiki.fasterxml.com/JacksonJAXBAnnotations

我认为这最终就是您要寻找的.Jackson 进行 Object<->JSON 处理...Jersey 只是为您拨打电话

I think that's ultimately what you are looking for. Jackson does Object<->JSON processing...Jersey just makes the calls for you