我正在建立一个自定义异常。
public class ValidationException extends RuntimeException {
public validationException(String errorId, String errorMsg) {
super(errorId, errorMsg);
}
}
这当然会引发错误
我也想在我的 globalexceptionalhandler 中获取错误和errormsg
ex.getMessage();
,但我希望函数能够分别获取错误和错误。如何实现?
您想进入 errorId
和 errorMsg
作为验证exception类的字段,就像您使用普通类一样。
public class ValidationException extends RuntimeException {
private String errorId;
private String errorMsg;
public validationException(String errorId, String errorMsg) {
this.errorId = errorId;
this.errorMsg = errorMsg;
}
public String getErrorId() {
return this.errorId;
}
public String getErrorMsg() {
return this.errorMsg;
}
}
和您的GlobalexceptionHandler:
@ExceptionHandler(ValidationException.class)
public ResponseEntity<SomeObject> handleValidationException(ValidationException ex) {
// here you can do whatever you like
ex.getErrorId();
ex.getErrorMsg();
}