【问题标题】:JAX-RS one mapper for all custom exceptionsJAX-RS 一个映射器,用于所有自定义异常
【发布时间】:2019-03-05 08:12:19
【问题描述】:

我创建了一个自定义异常,以在出现问题时通知用户。例如。如果用户从前端发送一些不正确的数据并检查是否发生在后端深处的某处而不是我需要做的只是为了

抛出新的 CustomException("Message", 406)

带有消息和错误代码。之后将立即向前端发送一个包含错误代码和文本消息的响应,我可以在 UI 上显示它们,例如作为小吃店。

自定义异常

public class CustomException extends RuntimeException {

  private final int errorCode;

  private static final long serialVersionUID = 6220614065727287629L;


  public CustomException(String message, int errorCode) {
    super(message);
    this.errorCode = errorCode;
  }

  int getErrorCode() {
    return errorCode;
  }
}

CustomExceptionMapper

@Provider
public class CustomExceptionMapper implements ExceptionMapper<CustomException> {

  @Override
  public Response toResponse(CustomException ex) {

    ErrorResponse response = new ErrorResponse(ex.getMessage(), ex.getErrorCode());
    return Response.status(ex.getErrorCode()).entity(response).build();
  }
}

错误响应

公共类错误响应 {

 private Error error;

  ErrorResponse(String errorMessage, int errorCode) {
    createError(errorMessage, errorCode);
  }

  private void createError(String message, int code) {
    setError(new Error(message, code));
  }

  public Error getError() {
    return error;
  }

  public void setError(Error error) {
    this.error = error;
  }

}

错误

public class Error {
  private String message;
  private int code;

  public Error(String message, int code) {
    this.message = message;
    this.code = code;
  }

  public String getMessage() {
    return message;
  }

  public void setMessage(String message) {
    this.message = message;
  }

  public int getCode() {
    return code;
  }

  public void setCode(int code) {
    this.code = code;
  }
}

但现在我需要一些额外的功能。对于后端的表单验证,我还需要发送哪个文本字段不正确。所以我认为最好的方法是创建一个新的 CustomException 子异常并在那里添加一个新属性。

CustomFormException

public class CustomFormException extends CustomException {

  private final String formField;

  private static final long serialVersionUID = 6220614065727287630L;


  public CustomFormException(String message, int errorCode, String formField) {
    super(message, errorCode);
    this.formField = formField;
  }

  String getFormField() {
    return formField;
  }
}

我可以稍微修改一下ErrorResponse 和Error 类。添加新的构造函数和新的属性 textField

public class ErrorResponse {

  private Error error;

  ErrorResponse(String errorMessage, int errorCode) {
    createError(errorMessage, errorCode);
  }

  ErrorResponse(String errorMessage, int errorCode, String textField) {
    createError(errorMessage, errorCode, textField);
  }

  private void createError(String message, int code) {
    setError(new Error(message, code));
  }

   private void createError(String message, int code, String textField) {
    setError(new Error(message, code, textField));
  }

  public Error getError() {
    return error;
  }

  public void setError(Error error) {
    this.error = error;
  }

}

错误

public class Error {
  private String message;
  private int code;
  private int String textField;

  public Error(String message, int code) {
    this.message = message;
    this.code = code;
  }

  public Error(String message, int code, String textField) {
    this.message = message;
    this.code = code;
    this.textField = textField;
  }

  // setters and getters
}

但是我还需要创建一个新的 Mapper,因为旧的 Mapper 是为 CustomException 制作的,而在 CustomException 中没有 textField 属性。

是否可以以某种方式为所有自定义异常使用一个映射器,而不是为每个自定义异常创建额外的映射器?

【问题讨论】:

    标签: java exception jax-rs


    【解决方案1】:

    您可以为所有自定义异常使用通用基类,并在异常映射器中使用基类。

    例如:

    public class MyServiceException extends RuntimeException {
    
      private ErrorCode errorCode;
      private boolean customMessage;
    
    
      public boolean haveCustomMessage() {
        return customMessage;
      }
    
      public MyServiceException(ErrorCode errorCode) {
        super(errorCode.getMessage());
        this.errorCode = errorCode;
      }
    
      public MyServiceException(ErrorCode errorCode, String msg) {
        super(msg);
        customMessage = true;
        this.errorCode = errorCode;
      }
    
      private static final long serialVersionUID = 5212119866822715497L;
    
    
      public HttpError getHttpError() {
    
        return new HttpError(errorCode);
      }
    }
    
    public class DataNotFoundException extends MyServiceException {
    
      private static final long serialVersionUID = 1L;
    
      public DataNotFoundException() {
        super(ErrorCode.DATA_NOT_FOUND);
      }
    
    }
    
    public class InternalServerException extends MyServiceException {
    
      private static final long serialVersionUID = 1L;
    
      public InternalServerException() {
        super(ErrorCode.INTERNAL_SERVER_ERROR);
      }
    
    }
    
    @Provider
    public class MyServiceExceptionMapper implements ExceptionMapper<MyServiceException> {
    
      @Override
      public Response toResponse(MyServiceException exception) {
        return Response.status(exception.getHttpError().getErrorCode().getCode())
            .entity(exception.getHttpError().getErrorCode().getMessage()).type("text/plain").build();
      }
    }
    
    public class HttpError {
    
      public HttpError(ErrorCode errorCode) {
        this.errorCode = errorCode;
      }
    
      private ErrorCode errorCode;
    
      public ErrorCode getErrorCode() {
        return errorCode;
      }
    
    }
    
    public enum ErrorCode {
    
      INTERNAL_SERVER_ERROR(
          Code.INTERNAL_SERVER_ERROR, Messages.INTERNAL_SERVER_ERROR), DATA_NOT_FOUND(Code.DATA_NOT_FOUND,
          "Data not found ");
    
    
      private int errorCode;
      private String errorMessage;
    
      private ErrorCode(int errorCode, String errorMessage) {
        this.errorCode = errorCode;
        this.errorMessage = errorMessage;
      }
    
      private interface Code {
    
        int DATA_NOT_FOUND = 404;
        int INTERNAL_SERVER_ERROR = 500;
      }
    
      public int getCode() {
        return errorCode;
      }
    
      public String getMessage() {
        return errorMessage;
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-10-07
      • 2011-03-18
      • 1970-01-01
      • 1970-01-01
      • 2016-11-01
      • 2019-10-05
      • 1970-01-01
      • 2011-03-14
      • 2013-02-17
      相关资源
      最近更新 更多