Rest Error Handling

This article shows how to create an error handling strategy for JAX-RS Runtime Exceptions.

Even though we can use any class that extends WebApplicationException from JAX-RS spec, there is a possibility to create custom exception mappers.

Exception Mapper for Runtime Exception

Just add this class to your project and you will be rocking a JSON/XML proper response.


import static;

@Produces({"application/json", "application/xml"})
public class RuntimeExceptionMapper implements ExceptionMapper<RuntimeException> {

    public Response toResponse(RuntimeException e) {
        return Response.status(INTERNAL_SERVER_ERROR)
                .entity(new Error(e.getMessage()))

    private static class Error implements Serializable {

        String msg;

        public Error() {

        public Error(String name) {
            this.msg = name;

After this we can safely throw Runtime Exceptions without exposing internal details to the client.

    public Response hello(@PathParam("param") String param) {
        throw new IllegalArgumentException("invalid param");

No dependencies needed. This will produce the following JSON:

	"msg": "invalid param"

Written on October 3, 2016