package com.howhy.basicAuth.config; import org.springframework.util.StringUtils; import java.util.HashMap; public class ResultVo extends HashMap<String,Object> { private static final String CODE_TAG="code"; private static final String MSG_TAG="msg"; private static final String DATA_TAG="data"; private ResultVo(){ } private ResultVo(int code,String msg){ super.put(CODE_TAG,code); super.put(MSG_TAG,msg); } private ResultVo(int code,String msg,Object data){ super.put(CODE_TAG,code); super.put(MSG_TAG,msg); if(!StringUtils.isEmpty(data)){ super.put(DATA_TAG,data); } } public static ResultVo success(String msg,Object data){ return new ResultVo(0,msg,data); } public static ResultVo success(String msg){ return ResultVo.success(msg,null); } public static ResultVo success(){ return ResultVo.success("操作成功"); } public static ResultVo error(String msg,Object data){ return new ResultVo(500,msg,data); } public static ResultVo error(String msg){ return ResultVo.success(msg,null); } public static ResultVo error(){ return ResultVo.success("操作失败"); } public static ResultVo error(int code,String msg){ return new ResultVo(code,msg); } public static void main(String[] args) { System.out.println(ResultVo.success("ok","444444444")); } }
package com.ruoyi.framework.web.exception; import javax.servlet.http.HttpServletRequest; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.security.access.AccessDeniedException; import org.springframework.validation.BindException; import org.springframework.web.HttpRequestMethodNotSupportedException; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; import com.ruoyi.common.constant.HttpStatus; import com.ruoyi.common.core.domain.ResultVo; import com.ruoyi.common.exception.DemoModeException; import com.ruoyi.common.exception.ServiceException; import com.ruoyi.common.utils.StringUtils; /** * 全局异常处理器 * * @author ruoyi */ @RestControllerAdvice public class GlobalExceptionHandler { private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class); /** * 权限校验异常 */ @ExceptionHandler(AccessDeniedException.class) public ResultVo handleAccessDeniedException(AccessDeniedException e, HttpServletRequest request) { String requestURI = request.getRequestURI(); log.error("请求地址'{}',权限校验失败'{}'", requestURI, e.getMessage()); return ResultVo.error(HttpStatus.FORBIDDEN, "没有权限,请联系管理员授权"); } /** * 请求方式不支持 */ @ExceptionHandler(HttpRequestMethodNotSupportedException.class) public ResultVo handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException e, HttpServletRequest request) { String requestURI = request.getRequestURI(); log.error("请求地址'{}',不支持'{}'请求", requestURI, e.getMethod()); return ResultVo.error(e.getMessage()); } /** * 业务异常 */ @ExceptionHandler(ServiceException.class) public ResultVo handleServiceException(ServiceException e, HttpServletRequest request) { log.error(e.getMessage(), e); Integer code = e.getCode(); return StringUtils.isNotNull(code) ? ResultVo.error(code, e.getMessage()) : ResultVo.error(e.getMessage()); } /** * 拦截未知的运行时异常 */ @ExceptionHandler(RuntimeException.class) public ResultVo handleRuntimeException(RuntimeException e, HttpServletRequest request) { String requestURI = request.getRequestURI(); log.error("请求地址'{}',发生未知异常.", requestURI, e); return ResultVo .error(e.getMessage()); } /** * 系统异常 */ @ExceptionHandler(Exception.class) public ResultVo handleException(Exception e, HttpServletRequest request) { String requestURI = request.getRequestURI(); log.error("请求地址'{}',发生系统异常.", requestURI, e); return ResultVo.error(e.getMessage()); } /** * 自定义验证异常 */ @ExceptionHandler(BindException.class) public ResultVo handleBindException(BindException e) { log.error(e.getMessage(), e); String message = e.getAllErrors().get(0).getDefaultMessage(); return ResultVo.error(message); } /** * 自定义验证异常 */ @ExceptionHandler(MethodArgumentNotValidException.class) public Object handleMethodArgumentNotValidException(MethodArgumentNotValidException e) { log.error(e.getMessage(), e); String message = e.getBindingResult().getFieldError().getDefaultMessage(); return ResultVo.error(message); } /** * 演示模式异常 */ @ExceptionHandler(DemoModeException.class) public ResultVo handleDemoModeException(DemoModeException e) { return ResultVo.error("演示模式,不允许操作"); } }