Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JAX-RS和EJB异常处理_Java_Exception Handling_Ejb_Jax Rs - Fatal编程技术网

Java JAX-RS和EJB异常处理

Java JAX-RS和EJB异常处理,java,exception-handling,ejb,jax-rs,Java,Exception Handling,Ejb,Jax Rs,我无法处理RESTful服务中的异常: @Path("/blah") @Stateless public class BlahResource { @EJB BlahService blahService; @GET public Response getBlah() { try { Blah blah = blahService.getBlah(); SomeUtil.doSomething();

我无法处理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);
        }
    }
}
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;
    }
}
下面是RestException的异常映射程序:

@Provider
public class RestExceptionMapper implements ExceptionMapper<RestException> {

    public Response toResponse(RestException e) {
        return Response.status(e.getStatus())
            .entity(getExceptionString(e.getMessage(), e.getReason()))
            .type("application/json")
            .build();
    }

    public String getExceptionString(String message, String reason) {
        JSONObject json = new JSONObject();
        try {
            json.put("error", message);
            json.put("reason", reason);
        } catch (JSONException je) {}
        return json.toString();
    }

}
@Provider
公共类RestExceptionMapper实现ExceptionMapper{
公众响应(RESTE例外){
返回Response.status(例如getStatus())
.entity(getExceptionString(如getMessage(),e.getReason())
.type(“应用程序/json”)
.build();
}
公共字符串getExceptionString(字符串消息、字符串原因){
JSONObject json=新的JSONObject();
试一试{
put(“错误”,消息);
json.put(“reason”,reason);
}捕获(JSONException je){}
返回json.toString();
}
}
现在,向最终用户提供响应代码和一些响应文本对我来说很重要。但是,当抛出RestException时,这会导致EJBException(带有消息“EJB抛出了一个意外(未声明的)异常…”)也被抛出,servlet只将响应代码返回给客户端(而不是我在RestException中设置的响应文本)

当我的RESTful资源不是EJB时,它可以完美地工作。。。有什么想法吗?我已经为此工作了好几个小时了,我已经没有主意了


谢谢

当RestException扩展javax.ws.rs.WebApplicationException时,类似的情况也适用于我。这个问题似乎与EJB异常处理有关。根据规范,从托管bean中抛出的任何
系统异常
(即,任何未明确标记为应用程序异常的RuntimeException)都将打包到EJBException中,然后,如果需要,打包到抛出到客户机的RemoteException中。这似乎是您所处的情况,为了避免这种情况,您可以:

  • 将RestException更改为选中的异常并按此方式处理
  • 在RestException上使用注释
  • 创建EJBExceptionMapper并从
    (RestfulException)e.getCause()提取所需信息

我有一个类似的用例,我的EJB抛出WebApplicationException,它可以工作。