Book Image

RESTful Java Web Services, Second Edition

Book Image

RESTful Java Web Services, Second Edition

Overview of this book

Table of Contents (17 chapters)
RESTful Java Web Services Second Edition
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Reporting errors using application exceptions


It is recommended to use a checked application exception for recoverable error scenarios. In this section, we will see how a checked exception can be used in a RESTful web API implementation.

Here is a checked business exception definition for use in the JAX-RS resource method:

//Business exception class
public class DeprtmentNotFoundBusinessException extends Exception{

    public DeprtmentNotFoundBusinessException(String message) {
        super(message);
    }

    public DeprtmentNotFoundBusinessException(String message, 
       Throwable cause) {
        super(message, cause);
    }

    //Rest of the implementation code go here
}

The following code snippet uses DeprtmentNotFoundBusinessException for reporting the DepartmentNotFound error to the caller:

@DELETE
@Path("departments/{id}")
public void remove(@PathParam("id") Short id) throws 
    DeprtmentNotFoundBusinessException {
    //Read department from data store for id
    Department department...