优点: 可以优化处理开发过程中针对异常情况返回给前端
当你看到如下的代码是不是会很头疼 那么可以使用自定义全局异常来解决这个事情
@RequestMapping("/sendCode")
public JsonResult<Integer> sendCode(@RequestBody CodeVO codeVO ) {
int res = loginService.sendCode(codeDto.getPhoneNumber());
if (res == 0) return new JsonResponse<>(StatusCode.SUCCESS, res);
if (res == -1) return new JsonResponse<>(StatusCode.TEL_WRONG_FORMAT, res);
if (res == -2) return new JsonResponse<>(StatusCode.MESSAGE_FAILED_TO_SEND, res);
if (res == -3) return new JsonResponse<>(StatusCode.LOGIN_MORE_FIVE, res);
if (res == -4) return new JsonResponse<>(StatusCode.WAIT_ONE_MINUTE, res);
return new JsonResult<>(StatusCode.INTERNAL_SERVER_ERROR, res);
}
第一步 自定义全局运行时异常
// 全局异常捕捉器
import com.indeep.common.result.StatusCode;
public class MyException extends RuntimeException {
private Integer code;
public InDeepException(String message, Throwable cause) {
super(message, cause);
}
public InDeepException(String message) {
super(message);
}
public InDeepException(String message,Integer code) {
super(message);
this.code=code;
}
public InDeepException(StatusCode statusCode) {
super(statusCode.msg);
this.code=statusCode.code;
}
public Integer getCode() {
return code;
}
}
第二步 自定义全局异常处理器-用来处理第一步创建的自定义异常
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import javax.servlet.http.HttpServletRequest;
@RestControllerAdvice
public class MyExceptionHandler {
@ExceptionHandler(MyException.class)
public ResponseEntity<Object> handleCustomFallbackException(InDeepException ex, HttpServletRequest request) {
// 处理自定义fallback异常
ErrorResponseBody errorResponseBody = new ErrorResponseBody(
System.currentTimeMillis(),
ex.getCode(),
ex.getMessage(),
request.getRequestURI()
);
System.out.println(errorResponseBody);
return new ResponseEntity<>(errorResponseBody, HttpStatus.INTERNAL_SERVER_ERROR);
}
// 可以添加其他异常处理器...
}
第三步 自定义异常返回体信息
一定要加get 方法 如果没有get方法后面ResponseEntity反序列化的时候会报错 会由于反序列化异常覆盖当前自定义异常导致 自定义异常失效
// 错误响应体类
public class ErrorResponseBody {
// 定义字段、构造函数和getter方法
private long timestamp;
private int status;
private String message;
private String path;
public ErrorResponseBody(long timestamp, int status, String message, String path) {
this.timestamp = timestamp;
this.status = status;
this.message = message;
this.path = path;
}
public long getTimestamp() {
return timestamp;
}
public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
// 省略getter方法
}
第四步 使用自定义异常
public String demo() {
throw new MyException (“这里是自定义异常”,315);
throw new MyException (StatusCode.NO_GOODS_MESSAGE);
}
展示效果
{
"timestamp": 1723602573092,
"status": 403,
"message": "物品不存在",
"path": "/test"
}