How To Generate 404 Resource Not Found in Jersey Rest Service

Many times you may need to handle request for which no data is found.  In REST terminology, anything on the server is called Resource.  So when a resource is not found on the server REST service should send back 404 status code to the client.

Let us see the code on how to send back 404 status code when data or resource is not found.

RestResourceNotFound.java

The code below is very simple.  First it generates a list which contains 2 books.  This service expects a book id in the parameter.  In case the book id matches what is present in the list it returns the book else it sends back 404 status which is the standard HTTP code for resource not found.   The code below is self explanatory.

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("/getBook")
public class RestResourceNotFound {
 
	@Path("{bookId}")
	@Produces(MediaType.APPLICATION_XML)
	@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;
	}
 
}

When proper book id is sent

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

Output

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

When trying to access unavailable resource

http://localhost:8080/RestWebService/rest/getBook/3

Output

<body>
 <h1>HTTP Status 404 - Not Found</h1>
 <HR size="1" noshade="noshade">
 <p>
 <b>type</b> Status report
 </p>
 <p>
 <b>message</b>
 <u>Not Found</u>
 </p>
 <p>
 <b>description</b>
 <u>The requested resource is not available.</u>
 </p>
 <HR size="1" noshade="noshade">
 <h3>Apache Tomcat/7.0.65</h3>
 </body>