/java/

JEE JAX-RS 2.0 ExceptionMapper

2014-06-18 23:08:54

GitHub: https://github.com/szalek/JEE-JAX-RS2.0-ExceptionMapper

package pl.btbw;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("orders")
public class OrdersResource {

    @GET
    @Path("/exception")
    @Produces(MediaType.APPLICATION_JSON)
    public Order exceptionTest() {
        throw new RuntimeException("Lorem Ipsum Exception");
    }

}
package core;

import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

@Provider
public class RuntimeExceptionMapper implements ExceptionMapper

<runtimeexception> {

    @Override
    public Response toResponse(RuntimeException e) {
        return Response
                .status(Status.INTERNAL_SERVER_ERROR)
                .header("x-reason", e.getMessage())
                .build();
    }
}
package core;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("shop")
public class AppConfiguration extends Application {
}