处理泽西的MultiException

时间:2015-03-04 15:35:23

标签: java exception-handling jersey-2.0

我们如何处理包含Throwable对象列表的MultiException泽西?

异常映射器技术在一个异常的情况下工作正常,但是如何处理多个异常?

1 个答案:

答案 0 :(得分:0)

您可以通过在MultiException上调用getErrors()来获取异常列表。 MultiException类中定义的方法是:

public List<Throwable> getErrors() {
    synchronized (this.lock) {
        return Collections.unmodifiableList(this.throwables);
    }
}

在您的映射器中,您可以使用此列表来创建错误响应。这是一个简单的映射器类,可用于处理MultiException中的所有异常:

public class MultiExceptionMapper implements ExceptionMapper<MultiException>{    
    private static final String TYPE="ERROR";
    private static final Status ERROR_STATUS=Status.INTERNAL_SERVER_ERROR;

    @Override
    public Response toResponse(MultiException exception) {      
        List<MyResponseError> myErrors=  new ArrayList<>();
        for (Throwable throwable:exception.getErrors()){
            MyResponseError error = new MyResponseError(throwable.getMessage());
            error.setType(TYPE);
            myErrors.add(error);
        }
        Status errorStatus = ERROR_STATUS;      
        ErrorsDTO errors = new ErrorsDTO(errorStatus,myErrors);     
        return errors.generateResponse();   
    }
}

错误响应由此ErrorsDTO类生成:

@XmlRootElement    
public class ErrorsDTO extends ResponseDTO {        
    private List<MyResponseError> errors;   
    private String status="ERROR";
    private transient Status errorStatus=Status.NOT_FOUND;  
    private int errorCode;

    /**
     * Default constructor is required to support serialization/deserialization
     */
    public ErrorsDTO(){

    }
    /**
     * If status is not provided set status as NOT_FOUND
     * @param error
     */
    public ErrorsDTO(MyResponseError error){
        this(Status.NOT_FOUND,error);
    }
    public ErrorsDTO(Status errorStatus, MyResponseError error){
        List<MyResponseError> errors = new ArrayList<>();
        errors.add(error);
        this.errorStatus= errorStatus;
        this.errors =errors;
    }
    public ErrorsDTO(List<MyResponseError> errors){
        this(Status.NOT_FOUND,errors);
    }

    public ErrorsDTO(Status errorStatus, List<MyResponseError> errors){
        if(errors==null){
            errors = new ArrayList<>();
        }
        this.errorStatus = errorStatus;
        this.errors = errors;
    }

    @Override
    public Response generateResponse() {
        if(this.errorStatus==null){
            this.errorStatus= Status.NOT_FOUND;
        }
        this.errorCode= this.errorStatus.getStatusCode();

        ResponseBuilder builder = Response.status(errorStatus); 
        builder.entity(this);
        builder.type(MediaType.APPLICATION_JSON);
        Response response = builder.build();
        return response;
    }
    @XmlElement(name="errors")
    public List<MyResponseError> getErrors() {
        return errors;
    }
    public void setErrors(List<MyResponseError> errors) {
        this.errors = errors;
    }
    @XmlElement
    public String getStatus() {
        return status;
    }
    public void setStatus(String status) {
        this.status = status;
    }
    @XmlTransient
    public Status getErrorStatus() {
        return errorStatus;
    }
    public void setErrorStatus(Status errorStatus) {
        this.errorStatus = errorStatus;
    }
    @XmlElement
    public int getErrorCode() {
        return errorCode;
    }
    public void setErrorCode(int errorCode) {
        this.errorCode = errorCode;
    }
}