Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is JAXB worth it for a handful of small POJOs?

I'm implementing a small RESTful web service, and figured I'd return XML blobs to represent the resources, which map fairly directly to some Java classes I have.

I could do custom XML encoders for each class, but I stumbled across JAXB (which I have never used), and it seemed like a clean way of avoiding having to write tedious encoding logic that I'd have to keep in synch if I add new properties to any of the classes.

So, my question is: is JAXB too heavyweight for something as simple as this? Is it worth using as just something to map a basic structure, collection of beans, etc. to an XML document?

edit: I'm using Jersey to build the service.

like image 772
Shawn D. Avatar asked Jul 26 '11 15:07

Shawn D.


People also ask

When should JAXB be used?

JAXB stands for Java Architecture for XML Binding. It provides mechanism to marshal (write) java objects into XML and unmarshal (read) XML into object. Simply, you can say it is used to convert java object into xml and vice-versa.

What is the advantage of JAXB?

JAXB simplifies access to an XML document from a Java program by presenting the XML document to the program in a Java format. The first step in this process is to bind the schema for the XML document into a set of Java classes that represents the schema.

What is marshal and Unmarshal in Java?

Marshalling is the process of transforming Java objects into XML documents. Unmarshalling is the process of reading XML documents into Java objects. The JAXBContext class provides the client's entry point to the JAXB API. It provides API for marshalling, unmarshalling and validating.

What is JAXB context?

The JAXBContext class provides the client's entry point to the JAXB API. It provides an abstraction for managing the XML/Java binding information necessary to implement the JAXB binding framework operations: unmarshal, marshal and validate.


1 Answers

Note: I'm the EclipseLink JAXB (MOXy) lead, and a member of the JAXB (JSR-222) expert group.

If you are using a JAX-RS implementation (Jersey, RESTeasy, Wink, etc) to create your RESTful service then JAXB is the default binding layer and integrates seamlessly:

For an example see:

  • http://bdoughan.blogspot.com/2010/08/creating-restful-web-service-part-35.html
  • http://wiki.eclipse.org/EclipseLink/Examples/MOXy/GettingStarted
like image 121
bdoughan Avatar answered Oct 23 '22 15:10

bdoughan