Exceptions in Java are a way to tell the client that an abnormal condition has occurred while executing specific statements in the code. In this section, you will see how a REST resource method handles exceptions.
The javax.ws.rs.core.Response.ResponseBuilder
class provides easy-to-use utility methods for creating the javax.ws.rs.core.Response
instance by using a builder pattern. The Response
instance can hold metadata, such as the HTTP status code, along with the entity body. The REST resource method can return the Response
object to report back the status of the REST API call to the caller.
For example, the following resource method returns HTTP 404 Not Found
(represented by the following Enum constant: Response.Status.NOT_FOUND
) if the department
entity object is not found in the data store:
@DELETE @Path("departments/{id}") public Response remove(@PathParam("id") Short id) { Department department = entityManager.find...