Spring boot捕获全局异常
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.validation.ValidationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import cn.com.threeInOneRoad.util.returnMessageUtil.ResponseMessage;
import cn.com.threeInOneRoad.util.returnMessageUtil.ResultEnum;
import cn.com.threeInOneRoad.util.returnMessageUtil.ResultUtil;
@RestControllerAdvice
public class CommonExceptionAdvice {
private static Logger logger = LoggerFactory.getLogger(CommonExceptionAdvice.class);
/**
* 400 缺少参数
* @param e
* @return
*/
@ResponseStatus(value=HttpStatus.BAD_REQUEST)
@ExceptionHandler(MissingServletRequestParameterException.class)
public ResponseMessage handleMissingServletRequestParameterException(MissingServletRequestParameterException e) {
logger.error("缺少请求参数", e);
return ResultUtil.error(ResultEnum.PARAM_ERROR.getCode(), ResultEnum.PARAM_ERROR.getMsg());
}
/**
* 400 - Bad Request
*/
@ResponseStatus(value=HttpStatus.BAD_REQUEST)
@ExceptionHandler(HttpMessageNotReadableException.class)
public ResponseMessage handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
logger.error("参数解析失败", e);
return ResultUtil.error(ResultEnum.PARAM_JIEXI_ERROR.getCode(),ResultEnum.PARAM_JIEXI_ERROR.getMsg());
}
/**
* 400 - Bad Request
*/
@ResponseStatus(value=HttpStatus.BAD_REQUEST)
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseMessage handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
logger.error("参数验证失败", e);
BindingResult result = e.getBindingResult();
FieldError error = result.getFieldError();
String field = error.getField();
String code = error.getDefaultMessage();
String message = String.format("%s:%s", field, code);
return ResultUtil.error(ResultEnum.PARAM_YANZHENG_ERROR.getCode(),ResultEnum.PARAM_YANZHENG_ERROR.getMsg()+","+message);
}
/**
* 400 - Bad Request
*/
@ResponseStatus(value=HttpStatus.BAD_REQUEST)
@ExceptionHandler(BindException.class)
public ResponseMessage handleBindException(BindException e) {
logger.error("参数绑定失败", e);
BindingResult result = e.getBindingResult();
FieldError error = result.getFieldError();
String field = error.getField();
String code = error.getDefaultMessage();
String message = String.format("%s:%s", field, code);
return ResultUtil.error(ResultEnum.PARAM_BANGDING_ERROE.getCode(),ResultEnum.PARAM_BANGDING_ERROE.getMsg()+","+message);
}
/**
* 400 - Bad Request
*/
@ResponseStatus(value=HttpStatus.BAD_REQUEST)
@ExceptionHandler(ConstraintViolationException.class)
public ResponseMessage handleServiceException(ConstraintViolationException e) {
logger.error("参数验证失败", e);
Set
ConstraintViolation<?> violation = violations.iterator().next();
String message = violation.getMessage();
return ResultUtil.error(ResultEnum.PARAM_YANZHENG_ERROR.getCode(),ResultEnum.PARAM_YANZHENG_ERROR.getMsg()+","+message);
}
/**
* 400 - Bad Request
*/
@ResponseStatus(value=HttpStatus.BAD_REQUEST)
@ExceptionHandler(ValidationException.class)
public ResponseMessage handleValidationException(ValidationException e) {
logger.error("参数验证失败", e);
return ResultUtil.error(ResultEnum.PARAM_YANZHENG_ERROR.getCode(),ResultEnum.PARAM_YANZHENG_ERROR.getMsg());
}
/**
* 405 - Method Not Allowed
*/
@ResponseStatus(value=HttpStatus.METHOD_NOT_ALLOWED)
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public ResponseMessage handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
logger.error("不支持当前请求方法", e);
return ResultUtil.error(ResultEnum.HTTP_SENDREQUEST_ERROR.getCode(),ResultEnum.HTTP_SENDREQUEST_ERROR.getMsg());
}
/**
* 415 - Unsupported Media Type
*/
@ResponseStatus(value=HttpStatus.UNSUPPORTED_MEDIA_TYPE)
@ExceptionHandler(HttpMediaTypeNotSupportedException.class)
public ResponseMessage handleHttpMediaTypeNotSupportedException(Exception e) {
logger.error("不支持当前媒体类型", e);
// return new AjaxResult().failure("content_type_not_supported");
return ResultUtil.error(ResultEnum.HTTP_SENDREQUEST_ERROR.getCode(),ResultEnum.HTTP_SENDREQUEST_ERROR.getMsg()+","+e.getMessage());
}
/**
* 500 - Internal Server Error
*/
// @ResponseStatus(value=HttpStatus.INTERNAL_SERVER_ERROR)
// @ExceptionHandler(ServiceException.class)
// public ResponseMessage handleServiceException(ServiceException e) {
// logger.error("业务逻辑异常", e);
// return ResultUtil.error(ResultEnum.THE_CODE_DO_ERROE.getCode(),ResultEnum.THE_CODE_DO_ERROE.getMsg()+","+e.getMessage());
// }
/**
* 500 - Internal Server Error
*/
@ResponseStatus(value=HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(Exception.class)
public ResponseMessage handleException(Exception e) {
logger.error("业务逻辑异常", e);
return ResultUtil.error(ResultEnum.THE_CODE_DO_ERROE.getCode(),ResultEnum.THE_CODE_DO_ERROE.getMsg()+","+e.getMessage());
}
/**
* 操作数据库出现异常:名称重复,外键关联
*/
@ResponseStatus(value=HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(DataIntegrityViolationException.class)
public ResponseMessage handleException(DataIntegrityViolationException e) {
logger.error("操作数据库出现异常:", e);
return ResultUtil.error(ResultEnum.SQL_ERROR.getCode(),ResultEnum.SQL_ERROR.getMsg()+","+e.getMessage());
}
}
返回值自定义