
com.itxiaoer.commons.web.ExceptionAdvice Maven / Gradle / Ivy
package com.itxiaoer.commons.web;
import com.itxiaoer.commons.core.FieldRepetitionException;
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.dao.EmptyResultDataAccessException;
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.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("The request method {} is not supported ", request.getMethod(), e);
return Response.failure(String.format("The request method %s is not supported ", request.getMethod()), ResponseCode.PARAMETER_VALID_CODE);
}
@ExceptionHandler(UsernameNotFoundException.class)
public Response usernameNotFoundException(UsernameNotFoundException e) {
log.error("User or password error: ", e);
return Response.failure(String.format("User or password error. %s", e.getMessage()), ResponseCode.LOGIN_PASSWORD_ERROR_CODE);
}
@ExceptionHandler({ValidationException.class})
@ResponseBody
public Response handleValidationException(ValidationException e) {
log.error("Parameter validation failed : ", e);
return Response.failure(String.format("Parameter validation failed : %s", e.getMessage()), ResponseCode.PARAMETER_VALID_CODE);
}
@ExceptionHandler({HttpMessageNotReadableException.class})
@ResponseBody
public Response handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
log.error("Parameter validation failed : ", e);
return Response.failure(String.format("Parameter validation failed : %s", e.getMessage()), ResponseCode.PARAMETER_VALID_CODE);
}
@ExceptionHandler({HttpMediaTypeNotSupportedException.class})
@ResponseBody
public Response handleHttpMediaTypeNotSupportedException(Exception e) {
log.error("The current media type is not supported: ", e);
return Response.failure("The request method %s is not supported ", ResponseCode.SERVER_ERROR_CODE);
}
@ExceptionHandler({MethodArgumentTypeMismatchException.class})
@ResponseBody
public Response handleHttpMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e) {
log.error("Parameter validation failed : ", e);
return Response.failure("Parameter validation failed : " + e.getMessage(), ResponseCode.PARAMETER_VALID_CODE);
}
@ExceptionHandler({NoHandlerFoundException.class})
@ResponseBody
public Response handleNoHandlerFoundException(NoHandlerFoundException e) {
log.error("Resources don't exist: ", e);
return Response.failure(String.format("Resources don't exist : %s", e.getMessage()), ResponseCode.NOT_FOUNT_CODE);
}
@ExceptionHandler({NullPointerException.class})
@ResponseBody
public Response handleNullPointerException(NullPointerException e) {
log.error("null point exception : ", e);
return Response.failure(String.format("Resources don't exist : %s", e.getMessage()), ResponseCode.SERVER_ERROR_CODE);
}
@ResponseBody
@ExceptionHandler({MethodArgumentNotValidException.class})
public Response handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
List fieldErrors = e.getBindingResult().getFieldErrors();
ArrayList
© 2015 - 2025 Weber Informatics LLC | Privacy Policy