How to reuse Jersey's JSON/JAXB for serialization?

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.

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 ObjectJSON processing...Jersey just makes the calls for you.

1 jackson.codehaus. Org for jackson project home – Andy O Nov 6 '09 at 23:06 Thanks, Andy. This indeed sounds like what I'm looking for.

I just don't want to be adding unnecessary additional dependencies. Thanks! – ctwomey Nov 8 '09 at 16:32 1 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 Nov 16 '09 at 19:37.

Here's a simple brief example of using JAXB to map objects to JSON (using Jackson): ondra.zizka.cz/stranky/programovani/java....

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... List myArray = new ArrayList(); ...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: jersey.java.net/nonav/documentation/late... 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!

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.

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 Jul 9 '09 at 18:55.

ObjectMapper mapper = new ObjectMapper(); String str = mapper. WriteValueAsString(pojoObject).

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 Nov 16 '09 at 19:40.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions