Spring boot捕獲全局異常

import java.util.Set;java

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.validation.ValidationException;web

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;spring

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 {code


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());
}orm

/**
* 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);
}three

/**
* 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);
}get

/**
* 400 - Bad Request
*/
@ResponseStatus(value=HttpStatus.BAD_REQUEST)
@ExceptionHandler(ConstraintViolationException.class)
public ResponseMessage handleServiceException(ConstraintViolationException e) {
logger.error("參數驗證失敗", e);
Set<ConstraintViolation<?>> violations = e.getConstraintViolations();
ConstraintViolation<?> violation = violations.iterator().next();
String message = violation.getMessage();
return ResultUtil.error(ResultEnum.PARAM_YANZHENG_ERROR.getCode(),ResultEnum.PARAM_YANZHENG_ERROR.getMsg()+","+message);
}it

/**
* 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());
}io

/**
* 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());
}
}

 

 

返回值自定義

相關文章
相關標籤/搜索