package com.yfp.web.exception;
import com.yfp.common.model.ResultResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.*;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.validation.ValidationException;
import java.util.Set;
/**
* 统一异常处理
* @author wangxuefei
*/
@RestControllerAdvice
public class GlobalExceptionController {
private static Logger logger = LoggerFactory.getLogger(GlobalExceptionController.class);
/**
* 400 - Bad Request
*/
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(MissingServletRequestParameterException.class)
public ResultResponse handleMissingServletRequestParameterException(MissingServletRequestParameterException e) {
logger.error("参数名称或类型不匹配", e);
return new ResultResponse().baseFailResult(HttpStatus.BAD_REQUEST.value(),"参数名称或类型不匹配。",null);
}
/**
* 400 - Bad Request
*/
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(HttpMessageNotReadableException.class)
public ResultResponse handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
logger.error("参数自动转换解析失败JSON", e);
return new ResultResponse().baseFailResult(HttpStatus.BAD_REQUEST.value(),"参数自动转换解析失败JSON", null);
}
/**
* 400 - Bad Request
*/
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResultResponse 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 new ResultResponse().baseFailResult(HttpStatus.BAD_REQUEST.value(), message, error);
}
/**
* 400 - Bad Request
*/
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(BindException.class)
public ResultResponse 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 new ResultResponse().baseFailResult(HttpStatus.BAD_REQUEST.value(), message, e.getAllErrors());
}
/**
* 400 - Bad Request
*/
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(ConstraintViolationException.class)
public ResultResponse handleServiceException(ConstraintViolationException e) {
logger.error("参数违反约束,验证失败", e);
Set<ConstraintViolation<?>> violations = e.getConstraintViolations();
ConstraintViolation<?> violation = violations.iterator().next();
String message = violation.getMessage();
return new ResultResponse().baseFailResult(HttpStatus.BAD_REQUEST.value(), message, e.getMessage());
}
/**
* 400 - Bad Request
*/
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(ValidationException.class)
public ResultResponse handleValidationException(ValidationException e) {
logger.error("参数验证失败", e);
return new ResultResponse().baseFailResult(HttpStatus.BAD_REQUEST.value(), "参数验证失败", null);
}
/**
* 405 - Method Not Allowed
*/
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public ResultResponse handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
logger.error("不支持当前请求方法", e);
return new ResultResponse().baseFailResult(HttpStatus.METHOD_NOT_ALLOWED.value(), "不支持当前请求方法", e.getMessage());
}
/**
* 415 - Unsupported Media Type
*/
@ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
@ExceptionHandler(HttpMediaTypeNotSupportedException.class)
public ResultResponse handleHttpMediaTypeNotSupportedException(HttpMediaTypeNotSupportedException e) {
logger.error("不支持当前请求内容类型", e);
return new ResultResponse().baseFailResult(HttpStatus.UNSUPPORTED_MEDIA_TYPE.value(), "不支持当前请求内容类型", e.getMessage());
}
/**
* 500 - Internal Server Error
*/
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(ServiceException.class)
public ResultResponse handleServiceException(ServiceException e) {
logger.error("业务逻辑异常", e);
return new ResultResponse().baseFailResult(HttpStatus.INTERNAL_SERVER_ERROR.value(), "业务功能异常", e.getMessage());
}
/**
* 500 - Internal Server Error
*/
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler
public ResultResponse handleException(Exception e) {
logger.error("系统异常", e);
return new ResultResponse().baseFailResult(HttpStatus.INTERNAL_SERVER_ERROR.value(), "系统异常", e.getMessage());
}
}
Spring Boot全局异常处理
最新推荐文章于 2025-02-10 07:30:00 发布