正文
Spring 统一异常处理有 3 种方式,分别为:
@ExceptionHandler:用于捕获所有控制器里面的异常,并进行处理。
@InitBinder:用来设置 WebDataBinder,WebDataBinder 用来自动绑定前台请求参数到 Model 中。
@ModelAttribute:@ModelAttribute 本来的作用是绑定键值对到 Model 里,此处是让全局的@RequestMapping 都能获得在此处设置的键值对。
本文使用 @ControllerAdvice + @ExceptionHandler 进行全局的 Controller 层异常处理。只要设计得当,就再也不用在 Controller 层进行 try-catch 了!
package com.tao.smp.exception;
/**
* Api统一的返回结果类
*/
public class ApiResult {
/**
* 结果码
*/
private String code;
/**
* 结果码描述
*/
private String msg;
public ApiResult() {
}
public ApiResult(ResultCode resultCode) {
this.code = resultCode.getCode();
this.msg = resultCode.getMsg();
}
/**
* 生成一个ApiResult对象, 并返回
*
* @param resultCode
* @return
*/
public static ApiResult of(ResultCode resultCode) {
return new ApiResult(resultCode);
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
@Override
public String toString() {
return "ApiResult{" +
"code='" + code + '\'' +
", msg='" + msg + '\'' +
'}';
}
}
package com.tao.smp.exception;
/**
* 错误码
*/
public enum ResultCode {
/**
* 成功
*/
SUCCESS("0", "success"),
/**
* 未知错误
*/
UNKNOWN_ERROR("0x10001", "unkonwn error"),
/**
* 用户名错误或不存在
*/
USERNAME_ERROR("0x10002", "username error or does not exist"),
/**
* 密码错误
*/
PASSWORD_ERROR("0x10003", "password error"),
/**
* 用户名不能为空
*/
USERNAME_EMPTY("0x10004", "username can not be empty");
/**
* 结果码
*/
private String code;
/**
* 结果码描述
*/
private String msg;
ResultCode(String code, String msg) {
this.code = code;
this.msg = msg;
}
public String getCode() {
return code;
}
public String getMsg() {
return msg;
}
}
package com.tao.smp.exception;
/**
* 自定义业务异常
*/
public class BusinessRuntimeException extends RuntimeException {
/**
* 结果码
*/
private String code;
/**
* 结果码描述
*/
private String msg;
/**
* 结果码枚举
*/
private ResultCode resultCode;
public BusinessRuntimeException(ResultCode resultCode) {
super(resultCode.getMsg());
this.code = resultCode.getCode();
this.msg = resultCode.getMsg();
this.resultCode = resultCode;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public ResultCode getResultCode() {
return resultCode;
}
public void setResultCode(ResultCode resultCode) {
this.resultCode = resultCode;
}
}
最后便是定义全局异常处理类。
@ControllerAdvice
指定该类为 Controller
增强类。@ExceptionHandler
自定捕获的异常类型。@ResponseBody
返回 json
到前端。package com.tao.smp.exception;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* 全局Controller层异常处理类
*/
@ControllerAdvice
public class GlobalExceptionResolver {
private static final Logger LOG = LoggerFactory.getLogger(GlobalExceptionResolver.class);
/**
* 处理所有不可知异常
*
* @param e 异常
* @return json结果
*/
@ExceptionHandler(Exception.class)
@ResponseBody
public ApiResult handleException(Exception e) {
// 打印异常堆栈信息
LOG.error(e.getMessage(), e);
return ApiResult.of(ResultCode.UNKNOWN_ERROR);
}
/**
* 处理所有业务异常
*
* @param e 业务异常
* @return json结果
*/
@ExceptionHandler(BusinessRuntimeException.class)
@ResponseBody
public ApiResult handleOpdRuntimeException(BusinessRuntimeException e) {
// 不打印异常堆栈信息
LOG.error(e.getMsg());
return ApiResult.of(e.getResultCode());
}
}
package com.tao.smp.controller;
import com.tao.smp.exception.BusinessRuntimeException;
import com.tao.smp.exception.ResultCode;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* 测试异常的抛出
*/
@Controller
@RequestMapping("/")
public class TestController {
/**
* 测试返回异常信息
* @return
*/
@GetMapping("/exception")
public String returnExceptionInfo() {
if (1 != 2) {
// 用户民错误或不存在异常
throw new BusinessRuntimeException(ResultCode.USERNAME_ERROR);
}
return "success";
}
}
其他:常用异常捕获
@ControllerAdvice
@ResponseBody
public class WebExceptionHandle {
private static Logger logger = LoggerFactory.getLogger(WebExceptionHandle.class);
/**
* 400 - Bad Request
*/
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(HttpMessageNotReadableException.class)
public ServiceResponse handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
logger.error("参数解析失败", e);
return ServiceResponseHandle.failed("could_not_read_json");
}
/**
* 405 - Method Not Allowed
*/
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public ServiceResponse handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
logger.error("不支持当前请求方法", e);
return ServiceResponseHandle.failed("request_method_not_supported");
}
/**
* 415 - Unsupported Media Type
*/
@ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
@ExceptionHandler(HttpMediaTypeNotSupportedException.class)
public ServiceResponse handleHttpMediaTypeNotSupportedException(Exception e) {
logger.error("不支持当前媒体类型", e);
return ServiceResponseHandle.failed("content_type_not_supported");
}
/**
* 500 - Internal Server Error
*/
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(Exception.class)
public ServiceResponse handleException(Exception e) {
if (e instanceof BusinessException){
return ServiceResponseHandle.failed("BUSINESS_ERROR", e.getMessage());
}
logger.error("服务运行异常", e);
e.printStackTrace();
return ServiceResponseHandle.failed("server_error");
}
}