import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.ObjectError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.List;
import java.util.Set;
@ControllerAdvice
public class CustomControllerAdvice {
private static final Logger LOGGER = LoggerFactory.getLogger(CustomControllerAdvice.class);
@ResponseBody
@ExceptionHandler({BaseException.class})
public CommonResponse<String> chargingBaseExceptionHandler(HttpServletRequest request, HttpServletResponse response, BaseException e) {
LOGGER.error("自定义异常:", e);
return CommonResponse.error(e.getCode(), e.getMessage());
}
@ResponseBody
@ExceptionHandler({Exception.class})
public CommonResponse<String> errorHandler(HttpServletRequest request, HttpServletResponse response, Exception e) {
LOGGER.error("内部服务异常:", e);
return CommonResponse.error(ErrorCode.BIZ_ERROR);
}
@ResponseBody
@ExceptionHandler({TinyplatCommonException.class})
public CommonResponse<String> response(HttpServletRequest request, HttpServletResponse response, TinyplatCommonException e) {
LOGGER.error("自定义异常信息:", e);
PlatStandardCode code = e.getCode();
return CommonResponse.error(code.getBasicCode(), code.getBasicDesc(), code.getSubCode(), code.getSubDesc());
}
@ResponseBody
@ExceptionHandler({ConstraintViolationException.class})
public CommonResponse<String> processConstraintViolationException(HttpServletResponse response, ConstraintViolationException e) {
LOGGER.error("参数校验异常:", e);
response.setStatus(200);
response.setContentType("application/json;charset=UTF-8");
StringBuilder sb = new StringBuilder("参数错误:[");
Set<ConstraintViolation<?>> constraintViolations = e.getConstraintViolations();
constraintViolations.forEach((s) -> {
sb.append(s.getMessage()).append(',');
});
sb.deleteCharAt(sb.length() - 1);
sb.append(']');
return CommonResponse.error(ErrorCode.PARAMS_MISSING.getCode(), sb.toString());
}
@ResponseBody
@ExceptionHandler({MethodArgumentNotValidException.class})
public CommonResponse<String> processMethodArgumentNotValidException(HttpServletResponse response, MethodArgumentNotValidException e) {
LOGGER.error("参数校验异常:", e);
response.setStatus(200);
response.setContentType("application/json;charset=UTF-8");
StringBuilder sb = new StringBuilder("参数错误:[");
List<ObjectError> set = e.getBindingResult().getAllErrors();
set.forEach((s) -> {
sb.append(s.getDefaultMessage()).append(',');
});
sb.deleteCharAt(sb.length() - 1);
sb.append(']');
return CommonResponse.error(ErrorCode.PARAMS_MISSING.getCode(), sb.toString());
}
@ResponseBody
@ExceptionHandler({HttpRequestMethodNotSupportedException.class})
public CommonResponse<String> processMethodNotSupportedException(HttpServletResponse response, HttpRequestMethodNotSupportedException e) {
LOGGER.error("调用方式异常:", e);
response.setStatus(200);
response.setContentType("application/json;charset=UTF-8");
return CommonResponse.error(ErrorCode.BIZ_ERROR.getCode(), "该方法不支持'" + e.getMethod() + "'方式调用。");
}
}
全局异常统一处理 @ControllerAdvice
最新推荐文章于 2024-07-20 21:56:23 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)