如何重用Jersey的JSON/JAXB进行序列化? [英] How to reuse Jersey's JSON/JAXB for serialization?

查看:76
本文介绍了如何重用Jersey的JSON/JAXB进行序列化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Jersey实施的JAX-RS REST服务. JAX-RS/Jersey的很酷的功能之一是,只需洒一些Java注释,便可以将POJO轻松转换为REST服务...包括使用JAXB注释将POJO转换为JSON的简单方法. /p>

现在,我希望能够将这种酷炫的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中访问它?谢谢!

解决方案

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

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

http://wiki.fasterxml.com/JacksonJAXBAnnotations

我认为这最终是您要寻找的.杰克逊执行Object<-> JSON处理...杰西只是为您打电话

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!

解决方案

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

这篇关于如何重用Jersey的JSON/JAXB进行序列化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆