
com.itxiaoer.commons.web.ExceptionAdvice Maven / Gradle / Ivy
package com.itxiaoer.commons.web;
import com.itxiaoer.commons.core.json.JsonUtil;
import com.itxiaoer.commons.core.page.Response;
import com.itxiaoer.commons.core.page.ResponseCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.validation.BindException;
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.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import org.springframework.web.servlet.NoHandlerFoundException;
import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolationException;
import javax.xml.bind.ValidationException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author : liuyk
*/
@Slf4j
@ControllerAdvice
@SuppressWarnings("unused")
public class ExceptionAdvice {
@ExceptionHandler({HttpRequestMethodNotSupportedException.class})
@ResponseBody
public Response handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e, HttpServletRequest request) {
log.error("不支持当前请求方法:{}", e);
return Response.failure(String.format("不支持%s请求方式:", request.getMethod()));
}
@ExceptionHandler(UsernameNotFoundException.class)
public Response usernameNotFoundException(UsernameNotFoundException e) {
log.error(e.getMessage(), e);
return Response.failure("登录信息验证失败:" + e.getMessage(), ResponseCode.NO_PERMISSION);
}
@ExceptionHandler({ValidationException.class})
@ResponseBody
public Response handleValidationException(ValidationException e) {
log.error("参数验证失败:{}", e);
return Response.failure("参数验证失败:" + e.getMessage(), ResponseCode.PARAMETER_VALID_CODE);
}
@ExceptionHandler({HttpMessageNotReadableException.class})
@ResponseBody
public Response handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
log.error("参数解析失败{}", e);
return Response.failure("参数解析失败:" + e.getMessage(), ResponseCode.PARAMETER_VALID_CODE);
}
@ExceptionHandler({HttpMediaTypeNotSupportedException.class})
@ResponseBody
public Response handleHttpMediaTypeNotSupportedException(Exception e) {
log.error("不支持当前媒体类型", e);
return Response.failure("不支持当前媒体类型");
}
@ExceptionHandler({MethodArgumentTypeMismatchException.class})
@ResponseBody
public Response handleHttpMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e) {
log.error("参数解析失败{}", e);
return Response.failure("参数解析失败:" + e.getMessage(), ResponseCode.PARAMETER_VALID_CODE);
}
@ExceptionHandler({NoHandlerFoundException.class})
@ResponseBody
public Response handleNoHandlerFoundException(NoHandlerFoundException e) {
log.error("资源不存在{}", e);
return Response.failure("资源不存在:" + e.getMessage());
}
@ExceptionHandler({NullPointerException.class})
@ResponseBody
public Response handleNullPointerException(NullPointerException e) {
log.error("空指针异常:{}", e);
return Response.failure("空指针异常:" + e.getMessage());
}
@ResponseBody
@ExceptionHandler({MethodArgumentNotValidException.class})
public Response handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
List fieldErrors = e.getBindingResult().getFieldErrors();
ArrayList
© 2015 - 2025 Weber Informatics LLC | Privacy Policy