JAX-RS RESTEasy has very good support for all kind of application activities which can be customized to desired level. One of such capability is Exception handling. To build custom exception handlers, JAX-RS providesExceptionMapper interface.
ExceptionMapper is a contract for a provider that maps Java exceptions to Response object. An implementation of this interface must be annotated with Provider to work correctly.
How to build custom exception handler
A sample implementation provider class looks like this:
packagecom.howtodoinjava.exception;
importjavax.ws.rs.core.Response;
importjavax.ws.rs.core.Response.Status;
importjavax.ws.rs.ext.ExceptionMapper;
importjavax.ws.rs.ext.Provider;
@Provider
publicclass MyApplicationExceptionHandler implementsExceptionMapper<MyApplicationException>
{
@Override
publicResponse toResponse(MyApplicationException exception)
{
returnResponse.status(Status.BAD_REQUEST).entity(exception.getMessage()).build();
}
}
Where the MyApplicationException.java is written as:
packagecom.howtodoinjava.exception;
importjava.io.Serializable;
publicclass MyApplicationException extendsException implementsSerializable
{
privatestatic final long serialVersionUID = 1L;
publicMyApplicationException() {
super();
}
publicMyApplicationException(String msg) {
super(msg);
}
publicMyApplicationException(String msg, Exception e) {
super(msg, e);
}
}
Demo usage of ExceptionMapper
To test the ExceptionMapper implementation, I have written following REST API.
packagecom.howtodoinjava.rest;
importjavax.ws.rs.GET;
importjavax.ws.rs.Path;
importjavax.ws.rs.PathParam;
importjavax.ws.rs.core.Response;
importorg.jboss.resteasy.spi.validation.ValidateRequest;
importcom.howtodoinjava.exception.MyApplicationException;
@Path("/rest")
publicclass UserService
{
@Path("/users/{id}")
@GET
@ValidateRequest
publicResponse getUserBId ( @PathParam("id") String id ) throwsMyApplicationException
{
//validate mandatory field
if(id == null)
{
thrownew MyApplicationException("id is not present in request !!");
}
//Validate proper format
try
{
Integer.parseInt(id);
}
catch(NumberFormatException e)
{
thrownew MyApplicationException("id is not a number !!");
}
//Process the request
returnResponse.ok().entity("User with ID " + id + " found !!").build();
}
}
Above API accepts the user id parameter in Integer format. If we pass the id in some other format which can not be parsed to Integer, it will throw MyApplicationException. Our exception mapper should be able to handle this.
1) Valid request http://localhost:8080/RESTEasyExceptionMapperDemo/rest/users/1
![Valid request to REST API](https://i-blog.csdnimg.cn/blog_migrate/8694ffe8012fa482fcbbdc6a55444921.png)
Valid request to REST API
2) Invalid request http://localhost:8080/RESTEasyExceptionMapperDemo/rest/users/abc
![Invalid request to REST API](https://i-blog.csdnimg.cn/blog_migrate/7206150971268a76b3d40e5380c9c958.png)
Invalid request to REST API
To download the sourcecode of above application, follow below given link.
Sourcecode Download
Happy Learning !!