How To Return Multiple Content Type From Same Method in Jersey REST Service

Till now we have seen examples of REST service which return different type of content such as XML or JSON.  However we used different methods to handle each content type.  In this example we will see how to return different types of contents from the same method.

As you will see it is very easy to do this with a common method.

MultipleContentFromServiceMethod.java

The key part is the @Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})

This annotation denotes that the method can output XML or JSON content.

You might be wondering how the method decides which type of content to return.  The is the the Accept header.  The client can pass Accept header values such as application/xml or application/json.  Depending on the value of the header, the response gets converted into proper content type and sent back to the client.  In case Accept header is not sent then application/xml is sent back as it has higher priority.

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.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 javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
 
import com.freetipscentral.domain.Book;
 
@Path("/getContent")
public class MultipleContentFromServiceMethod {
 
	@Path("{bookId}")
	@Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
	@GET
	public Response getBook(@Context HttpHeaders headers, @PathParam("bookId") int bookId) {
 
		List<Book> books = getBooks();
		for(Book book : books) {
			if(book.getSerialNumber() == bookId) {
				return Response.ok(book).build();
			}
		}
		return Response.status(Status.NOT_FOUND).build();
 
	}
 
	private List<Book> getBooks() {
		List<Book> books = new ArrayList<Book>();
		Book book1 = new Book();
		book1.setName("Head First Java");
		book1.setSerialNumber(1);
 
		Book book2 = new Book();
		book2.setName("The Clean Coder");
		book2.setSerialNumber(2);
 
		books.add(book1);
		books.add(book2);
 
		return books;	
	}
}

Book.java

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

Url to Invoke

http://localhost:8080/RestWebService/rest/getContent/1

Output With Accept header value application/xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<book>
<name>Head First Java</name>
<serialNumber>1</serialNumber>
</book>

Output With Accept header value application/json

{
"name": "Head First Java",
"serialNumber": 1
}