所有自定义异常的JAX-RS一个映射器

时间:2019-03-05 08:12:20

标签: java exception jax-rs

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

  

抛出新的CustomException(“ Message”,406)

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

CustomException

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();
  }
}

ErrorResponse

公共类ErrorResponse {

 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属性。

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

1 个答案:

答案 0 :(得分:0)

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

例如:

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;
  }
}