在应用开发过程当中,除系统自身的异常外,不一样业务场景中用到的异常也不同,为了与标题 轻松搞定全局异常 更加的贴切,定义个本身的异常,看看如何捕获…java
package com.baizhi.exception;
public class CustomException extends RuntimeException {
private static final long serialVersionUID = 4564124491192825748L;
private int code;
public CustomException() {
super();
}
public CustomException(int code, String message) {
super(message);
this.setCode(code);
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
}
复制代码
异常信息模板web
定义返回的异常信息的格式,这样异常信息风格更为统一spring
public class ErrorResponseEntity {
private int code;
private String message;
// 省略 get set
}
复制代码
注解概述bash
Controller
层抛出的异常,若是添加 @ResponseBody
返回信息则为JSON
格式。@ControllerAdvice
与 @ResponseBody
的结合体。建立一个 GlobalExceptionHandler
类,并添加上 @RestControllerAdvice
注解就能够定义出异常通知类了,而后在定义的方法中添加上 @ExceptionHandler
便可实现异常的捕捉…mvc
package com.baizhi.controller;
import com.baizhi.exception.CustomException;
import com.baizhi.exception.ErrorResponseEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@RestControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
/**
* 定义要捕获的异常 能够多个 @ExceptionHandler({})
*
* @param request request
* @param e exception
* @param response response
* @return 响应结果
*/
@ExceptionHandler(CustomException.class)
public ErrorResponseEntity customExceptionHandler(HttpServletRequest request, final Exception e, HttpServletResponse response) {
response.setStatus(HttpStatus.BAD_REQUEST.value());
CustomException exception = (CustomException) e;
return new ErrorResponseEntity(exception.getCode(), exception.getMessage());
}
/**
* 捕获 RuntimeException 异常
* TODO 若是你以为在一个 exceptionHandler 经过 if (e instanceof xxxException) 太麻烦
* TODO 那么你还能够本身写多个不一样的 exceptionHandler 处理不一样异常
*
* @param request request
* @param e exception
* @param response response
* @return 响应结果
*/
@ExceptionHandler(RuntimeException.class)
public ErrorResponseEntity runtimeExceptionHandler(HttpServletRequest request, final Exception e, HttpServletResponse response) {
response.setStatus(HttpStatus.BAD_REQUEST.value());
RuntimeException exception = (RuntimeException) e;
return new ErrorResponseEntity(400, exception.getMessage());
}
/**
* 通用的接口映射异常处理方
*/
@Override
protected ResponseEntity<Object> handleExceptionInternal(Exception ex, Object body, HttpHeaders headers,
HttpStatus status, WebRequest request) {
if (ex instanceof MethodArgumentNotValidException) {
MethodArgumentNotValidException exception = (MethodArgumentNotValidException) ex;
return new ResponseEntity<>(new ErrorResponseEntity(status.value(), exception.getBindingResult().getAllErrors().get(0).getDefaultMessage()), status);
}
if (ex instanceof MethodArgumentTypeMismatchException) {
MethodArgumentTypeMismatchException exception = (MethodArgumentTypeMismatchException) ex;
logger.error("参数转换失败,方法:" + exception.getParameter().getMethod().getName() + ",参数:" + exception.getName()
+ ",信息:" + exception.getLocalizedMessage());
return new ResponseEntity<>(new ErrorResponseEntity(status.value(), "参数转换失败"), status);
}
return new ResponseEntity<>(new ErrorResponseEntity(status.value(), "参数转换失败"), status);
}
}
复制代码