我在RESTful服务中处理异常时遇到问题:
@Path("/blah")
@Stateless
public class BlahResource {
@EJB BlahService blahService;
@GET
public Response getBlah() {
try {
Blah blah = blahService.getBlah();
SomeUtil.doSomething();
return blah;
} catch (Exception e) {
throw new RestException(e.getMessage(), "unknown reason", Response.Status.INTERNAL_SERVER_ERROR);
}
}
}
Run Code Online (Sandbox Code Playgroud)
RestException是一个映射异常:
public class RestException extends RuntimeException {
private static final long serialVersionUID = 1L;
private String reason;
private Status status;
public RestException(String message, String reason, Status status) {
super(message);
this.reason = reason;
this.status = status;
}
}
Run Code Online (Sandbox Code Playgroud)
这是RestException的异常映射器:
@Provider
public class RestExceptionMapper …Run Code Online (Sandbox Code Playgroud)