JAX-RS Exception Handling - Quiz

Total: 6 questions

1. 

Special JAX-RS RuntimeException class, that allows to abort JAX-RS service method.

WebApplicationException class.

2. 

How to map an existing exception to a response?

It is used a custom exception mapping provider, which implements the ExceptionMapper<E extends Throwable> interface.

3. 

Example of mapping exception to a HTTP 404 response.

@Provider
public class NotFoundMapper implements ExceptionMapper<javax.persistence.NotFoundException> {
  public Response toResponse(javax.persistence.NotFoundException ex) {
    return Response.status(404).
      entity(ex.getMessage()).
      type("text/plain").
      build();
  }
}
4. 

When toResponse from this code will be invoked? 

@Provider
public class NotFoundMapper implements ExceptionMapper<javax.persistence.NotFoundException> {
  public Response toResponse(javax.persistence.NotFoundException ex) {
    return Response.status(404).
      entity(ex.getMessage()).
      type("text/plain").
      build();
  }
}

When a NotFoundException is thrown.

5. 

How to handle exceptions with JAX-RS?

The JAX-RS provides two possibilities for restful web services exception handling - WebApplicationException and mapping exceptions to responses.

6. 

What NotFoundException is used for?

It builds a HTTP response with the 404 status code and an optional message as the body of the response. The NotFoundException extends WebApplicationException.

Page 1 of 1