In this article we are going to see how you can integrate JAX-RS Jersey with JAXB (Java Architecture for XML Binding) to create RESTful services that produce XML streams. As you probably know JAXB is used to marshal a Java Object to XML, and ummarshal an XML file (or stream in general) to Java Object. You don’t have to download any additional libraries to use JAXB, as long as you use Jersey on your project.

In this example we are not going to focus on how to create JAX-RS application from top to bottom. So make sure you read carefully Jersey Hello World Example and pay attention to the sections concerning the creation of the project with Eclipse IDE.

Steps Need to be Followed
  • Add ‘jersey-server.jar‘ to your Maven pom.xml which includes all JAXB supporting libraries into your class path
  • Annotate your service method with @Produces(MediaType.APPLICATION_XML)

At this point you can also take a look at the web.xml file to see how the project is configured:

Here is the final pom.xml file having required dependencies.

This is the Object that is going to be represented in XML.

In the above code:

  • @XmlRootElement: defines the root element of XML.
  • @XmlElement: is used to define element in XML file.
  • @XmlAttribute: is used to define an attribute of the root element.

Let’s see how easy it is with Jersey to produce XML output using a simple Student.java instance.

After deploying the application, open your browser and go to:http://localhost:8080/WebServices_JAX_RS/rest/users/111

Output

It's good to share...Share on FacebookTweet about this on TwitterShare on LinkedInPin on PinterestShare on Google+Email this to someone

Leave a Reply

Your email address will not be published. Required fields are marked *