Rest Service Which Produces XML Or JSON Based on Accept Header

In the previous tutorials we saw how to write Rest web service with Jersey to get XML and JSON response.  How about creating a single web service which returns XML or JSON response based on headers values passed along with the request.  That way single class could handle the various responses.  You will learn that in this tutorial.

Client can send what kind of data they want based on Accept header.  You can easily set the header type in rest client such as Postman and then test the response you get.

RestAcceptHeaderExample.java

This class has two methods getXMLBook and getJSONBook.  Both of them contain the same code.  The main difference is in the Produces annotation on the top of the class.  Jersey will redirect the request to one of the methods based on the Accept header.  Accept header can take values such as application/xml or application/json.  Based on what is sent in the header  getXMLBook or getJSONBook methods will be called and proper response type will be returned.

package com.freetipscentral;
 
import java.util.ArrayList;
import java.util.List;
 
import javax.ws.rs.GET;
import javax.ws.rs.Path;
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;
import com.freetipscentral.domain.Item;
import com.freetipscentral.domain.Order;
 
@Path("/bookList")
public class RestAcceptHeaderExample {
 
	@GET
	@Produces(MediaType.APPLICATION_XML)
	public Order getXMLBook(@Context HttpHeaders headers) {
		List<Item> items = getItems();
 
		Order order = new Order();
		order.setItems(items);
 
		return order;
	}
 
	@GET
	@Produces(MediaType.APPLICATION_JSON)
	public Order getJSONBook(@Context HttpHeaders headers) {
		List<Item> items = getItems();
 
		Order order = new Order();
		order.setItems(items);
 
		return order;
	}
 
 
	private List<Item> getItems() {
		Book book1 = new Book("Harry Potter",1);
		Book book2 = new Book("Guliver Travels",2);
 
		Item item1 = new Item();
		item1.setBook(book1);
		item1.setQuantity(2);
 
		Item item2 = new Item();
		item2.setBook(book2);
		item2.setQuantity(4);
 
		List<Item> items = new ArrayList<Item>();
		items.add(item1);
		items.add(item2);
		return items;
	}
}

Here are the other classes.

Order class contains list of items.  Each Item contains Book object and order quantity.

Order.java

package com.freetipscentral.domain;
 
import java.util.List;
 
import javax.xml.bind.annotation.XmlRootElement;
 
@XmlRootElement
public class Order {
	List<Item> items;
 
	public List<Item> getItems() {
		return items;
	}
 
	public void setItems(List<Item> items) {
		this.items = items;
	}
}

Item.java

package com.freetipscentral.domain;
 
public class Item {
	Book book;
	int quantity;
 
	public Book getBook() {
		return book;
	}
	public void setBook(Book book) {
		this.book = book;
	}
	public int getQuantity() {
		return quantity;
	}
	public void setQuantity(int quantity) {
		this.quantity = quantity;
	}
}

Book.java

package com.freetipscentral.domain;
 
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;
	}
 
}

[/codesyntax]

Here is how to access the service

http://localhost:8080/RestWebService/rest/bookList

When the Accept header is application/xml

Here is the output

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<order>
    <items>
        <book>
            <name>Harry Potter</name>
            <serialNumber>1</serialNumber>
        </book>
        <quantity>2</quantity>
    </items>
    <items>
        <book>
            <name>Guliver Travels</name>
            <serialNumber>2</serialNumber>
        </book>
        <quantity>4</quantity>
    </items>
</order>

 

When the Accept header is application/json

Here is the output

{"items":[{"book":{"name":"Harry Potter","serialNumber":1},"quantity":2},{"book":{"name":"Guliver Travels","serialNumber":2},"quantity":4}]}