小编bob*_*nes的帖子

JAX-RS和EJB异常处理

我在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)

java ejb exception-handling jax-rs

7
推荐指数
2
解决办法
2761
查看次数

标签 统计

ejb ×1

exception-handling ×1

java ×1

jax-rs ×1