diff --git a/crm-common/src/main/java/com/yb/lb/common/exception/GlobalExceptionHandler.java b/crm-common/src/main/java/com/yb/lb/common/exception/GlobalExceptionHandler.java index 25118cf..2abeadd 100644 --- a/crm-common/src/main/java/com/yb/lb/common/exception/GlobalExceptionHandler.java +++ b/crm-common/src/main/java/com/yb/lb/common/exception/GlobalExceptionHandler.java @@ -5,13 +5,18 @@ import com.yb.lb.common.utils.ErrorUtils; import com.yb.lb.common.utils.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.TypeMismatchException; import org.springframework.stereotype.Component; import org.springframework.validation.BindException; +import org.springframework.web.HttpRequestMethodNotSupportedException; import org.springframework.web.bind.MethodArgumentNotValidException; +import org.springframework.web.bind.MissingServletRequestParameterException; 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; + /** * 全局异常处理器 * @@ -72,7 +77,53 @@ public class GlobalExceptionHandler { public Object validExceptionHandler(MethodArgumentNotValidException e) { log.error(e.getMessage(), e); String message = e.getBindingResult().getFieldError().getDefaultMessage(); - return AjaxResult.error(message); + return AjaxResult.error(ErrorUtils.ERROR, message); } + /** + * 请求方式不支持 + */ + @ResponseBody + @ExceptionHandler(HttpRequestMethodNotSupportedException.class) + public AjaxResult handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException e, + HttpServletRequest request) { + String requestURI = request.getRequestURI(); + log.error("请求地址'" + requestURI + "',不支持'" + e.getMethod() + "'请求"); + return AjaxResult.error(ErrorUtils.ERROR, e.getMessage()); + } + + /** + * 缺少参数异常 + */ + @ResponseBody + @ExceptionHandler(MissingServletRequestParameterException.class) + public AjaxResult handleMissingPathVariableException(MissingServletRequestParameterException e, HttpServletRequest request) { + String requestURI = request.getRequestURI(); + log.error("请求缺少必需的参数'" + requestURI + "',发生系统异常." + e.getMessage()); + return AjaxResult.error(ErrorUtils.ERROR, String.format("请求缺少必需的参数[%s]", e.getParameterName())); + } + + /** + * 请求参数类型不匹配 + */ + @ResponseBody + @ExceptionHandler(TypeMismatchException.class) + public AjaxResult handleMethodArgumentTypeMismatchException(TypeMismatchException e, HttpServletRequest request) { + String requestURI = request.getRequestURI(); + log.error("请求参数类型不匹配'" + requestURI + "',发生系统异常." + e.getMessage()); + return AjaxResult.error(ErrorUtils.ERROR, String.format("请求参数类型不匹配,参数[%s]要求类型为:'%s',但输入值为:'%s'", e.getPropertyName(), e.getRequiredType().getName(), e.getValue())); + } + + /** + * 拦截未知的运行时异常 + */ + @ResponseBody + @ExceptionHandler(RuntimeException.class) + public AjaxResult handleRuntimeException(RuntimeException e, HttpServletRequest request) { + String requestURI = request.getRequestURI(); + log.error("请求地址'" + requestURI + "',发生未知异常." + e.getMessage()); + return AjaxResult.error(ErrorUtils.ERROR, e.getMessage()); + } + + }