views:

7195

answers:

4

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!

A: 

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.

Jack Cox
I don't think I was very clear. The thing is, Jersey does already translate JAXB objects to JSON. I'm not looking for Jersey to add "serialization" support, but rather to gain access to the Provider functionality it already has for my *own* serialization code. I will update my question to add some clarity. Thanks!
ctwomey
A: 

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:

public class Model { @Property height; @Property weight; @Property age; }

No default constructor, no getter/setter noise, just a POJO with my own annotations.

I don't think this is JAXB fault though: JAXB is API for XML, not for other formats. But question then is whether JAXB API should be bent to work for other formats -- if so, work-arounds are needed. XML != JSON.Btw: Jackson that was already mentioned allows non-Bean POJOs to be used; can use fields or getters/setters, use actual real constructors and so on. It can use JAXB annotations as extra optional information if user really wants that. But that does not need them.
StaxMan
+4  A: 

Hi:

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

Andy O
http://jackson.codehaus.org/ for jackson project home
Andy O
Thanks, Andy. This indeed sounds like what I'm looking for. I just don't want to be adding unnecessary additional dependencies. Thanks!
ctwomey
Well, think of it this way: you need a library for XML serialization (JAXB, XStream or such).You need a JSON library for JSON: JAXB does not provide it; Jersey also dispatches it to a library. So question is rather which lib(s) to add; not whether you need to add something.So Jackson (or json-tools, gson) can do it easily. And JAX-RS providers are really little more than dispatchers that operation on media types, choosing which "view" to present (json, xml, ...), then calling appropriate library.
StaxMan
+2  A: 

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

Ondra Žižka