O'Reilly logo

RESTful Java Patterns and Best Practices by Bhakti Mehta

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Error handling with RESTful services

When building RESTful APIs, it is necessary to throw application-specific exceptions and provide specific HTTP responses containing the details of these exceptions. The following section covers how to deal with user-defined exceptions and map them to HTTP responses and status codes. The javax.ws.rs.ext.ExceptionMapper classes are custom, application provided, components that catch thrown application exceptions and write specific HTTP responses. Exception mapper classes are annotated with the @Provider annotation.

The following snippets show how to build your custom exception mapper:

 @GET @Produces(MediaType.APPLICATION_JSON) @Path("/orders/{id}") public Response getCoffee(@PathParam("id") int id) { Coffee coffee ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required