How To Create a Rest Web Service with XML response in Jersey

In a previous tutorial we learnt how to create a hello world rest service with JSON response.  This tutorial is a continuation of the previous tutorial.  Here you will learn how to get a XML response from Rest web service.  Ensure that you have a work space setup similar to the one you had in the previous tutorial.  The below example will show how to get a XML object from  Rest Service.

In case you want to see how to set up the workspace you can see it here.

Here are the steps.

  1. Write a Rest Service which returns a Book object.
  2. Create a Book domain object.
  3. Run the application and access the service.

Book.java

This class will be returned as XML object.

Things to remember for Book class.

  1. To return this object as XML response we need to annotate the class with @XmlRootElement which is part of javax,xml.bind.annotation package.
  2. Make sure it has a default constructor else you will get exception.
  3. Ensure getters are public else you may get blank xml response from jersey.
package com.freetipscentral.domain;
 
import javax.xml.bind.annotation.XmlRootElement;
 
@XmlRootElement
public class Book {
 
	String name;
	int serialNumber;
 
	public Book() {}
 
	public Book(String name, int serialNumber) {
		this.name = name;
		this.serialNumber = serialNumber;
	}
 
	public String getName() {
		return name;
	}
 
	public void setName(String name) {
		this.name = name;
	}
 
	public int getSerialNumber() {
		return serialNumber;
	}
 
	public void setSerialNumber(int serialNumber) {
		this.serialNumber = serialNumber;
	}
 
	public String toString() {
		return serialNumber + " "  + name;
	}
 
}

 

JerseyRestServiceWithXML.java  – the Rest Service

Things to remember for JerseyRestServiceWithXML class.

  1. @Path annotation for the class  has the value “/xmlBooks”.  While accessing the service this path should be used.
  2. @Produces(MediaType.APPLICATION_XML) annotation ensures that XML will be returned as the response.
package com.freetipscentral;
 
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
 
import com.freetipscentral.domain.Book;
 
@Path("/xmlBooks")
public class JerseyRestServiceWithXML {
 
	@GET
	@Produces(MediaType.APPLICATION_XML)
	@Path("{bookId}")
	public Book getBook(@Context HttpHeaders headers, @PathParam("bookId") int bookId) {
		Book book = null;
		Book book1 = new Book("Harry Potter",1);
		Book book2 = new Book("Harry Potter",2);
 
		if(bookId == 1) {
			book = book1;
		} else {
			book = book2;
		}
		return book;
	}
}

Now deploy the service and access the app with the following url – http://localhost:8080/RestWebService/rest/xmlBooks/1

The output will be –

<book>
<name>Harry Potter</name>
<serialNumber>1</serialNumber>
</book>

 

One thought to “How To Create a Rest Web Service with XML response in Jersey”

Comments are closed.