SpringBoot中这样定义全局异常处理器Global Exception Handler

在SpringBoot中,我们使用@ControllerAdvice注解定义全局异常处理器。详见下方代码块:

@ControllerAdvice
public class GlobalExceptionHandler {

 private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

 @ExceptionHandler(MissingServletRequestParameterException.class)
 @ResponseBody
 public GeneralResponse handleMissingServletRequestParameterException(MissingServletRequestParameterException e,
 HttpServletRequest request) {
 String message = "缺失请求参数" + e.getParameterName();
 return ackTransfer(request, message, HttpStatus.BAD_REQUEST.value(), e);
 }

 @ExceptionHandler(MethodArgumentTypeMismatchException.class)
 @ResponseBody
 public GeneralResponse handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e,
 HttpServletRequest request) {
 String message = "请求参数" + e.getName() + "类型错误";
 return ackTransfer(request, message, HttpStatus.BAD_REQUEST.value(), e);
 }

 @ExceptionHandler(HttpMessageConversionException.class)
 @ResponseBody
 public GeneralResponse handleHttpMessageNotReadableException(HttpMessageConversionException e,
 HttpServletRequest request) {
 String message = "参数类型错误";
 return ackTransfer(request, message, HttpStatus.BAD_REQUEST.value(), e);
 }

 /**
 * Default Exception Handler
 */
 @ExceptionHandler(Exception.class)
 @ResponseBody
 public GeneralResponse handleException(Exception e, HttpServletRequest request) {
 return ackTransfer(request, e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR.value(), e, true);
 }

 private GeneralResponse ackTransfer(HttpServletRequest request, String message, int code, Exception e,
 boolean printStackTrace) {
 GeneralResponse response = AckTransfer.fail(message, code);
 if (printStackTrace) {
 logger.error(message, e);
 } else {
 logger.error(message);
 }
 return response;
 }

 /**
 * transfer ACK
 */
 private GeneralResponse ackTransfer(HttpServletRequest request, String message, int code, Exception e) {
 return ackTransfer(request, message, code, e, false);
 }

}

以上,就是全局异常处理器的大体结构。

@ExceptionHandler用于绑定我们自定义的异常类(或者说我们需要catch住并处理的异常类,异常类可以自定义)。例如:我们在项目中经常需要抛出一些业务异常,此处,我们定义通用业务异常类为CommonBusinessException.class。那么,应该往GlobalExceptionHandler类添加如下方法定义:

/**
 * 通用业务异常
 */
@ExceptionHandler(CommonBusinessException.class)
@ResponseBody
public Response handleCommonBusinessException(CommonBusinessException e, HttpServletRequest request) {
 return ackTransfer(request, e.getMessage(), e.getCode(), e);
}

最后,附上GeneralResponse和AckTransfer的定义:

public class AckTransfer {

 private static final int EXCEPTION_CODE = 0;

 public static GeneralResponse fail(String message, int code) {
 return new GeneralResponse(message, code);
 }

 public static GeneralResponse fail(String message) {
 return new GeneralResponse(message, EXCEPTION_CODE);
 }

}

public class GeneralResponse {

 private static final String SUCCESS = "success";

 private static final String FAILED = "failed";

 // 状态描述
 private String status;
 // 具体响应内容
 private Object data;
 // 提示信息描述
 private String message;
 // 状态码
 private int code;

 GeneralResponse(Object data) {
 this.status = SUCCESS;
 this.data = data;
 this.message = "";
 this.code = 200;
 }

 GeneralResponse(String message, int code) {
 this.status = FAILED;
 this.data = null;
 this.message = message;
 this.code = code;
 }
 // Getter、Setter省略...

}

如果觉得文章对您有所助益,欢迎转发收藏!下方是本人头条号,期待您的关注!最新博文首发于头条号!

你可能感兴趣的:(Java)