异常类处理
- 添加maven依赖
- 创建自定义异常类
- 创建全局捕获异常
- 使用自定义异常类
maven文件:
<!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <!--shiro--> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-web</artifactId> <version>1.5.3</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>1.5.3</version> </dependency>
创建自定义异常:
package com.resume.uniapp.util.exception; public class EmosException extends RuntimeException { private String msg; private int code = 500; public EmosException(String msg) { super(msg); this.msg = msg; } public EmosException(String msg, Throwable e) { super(msg, e); this.msg = msg; } public EmosException(String msg, int code) { super(msg); this.msg = msg; this.code = code; } public EmosException(String msg, int code, Throwable e) { super(msg, e); this.msg = msg; this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public int getCode() { return code; } public void setCode(int code) { this.code = code; } }
创建全局捕获异常:
package com.resume.uniapp.util; import com.resume.uniapp.util.exception.EmosException; import lombok.extern.slf4j.Slf4j; import org.apache.shiro.authz.UnauthorizedException; import org.springframework.http.HttpStatus; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestControllerAdvice; @Slf4j @RestControllerAdvice public class ExceptionAdvice { @ResponseBody @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) @ExceptionHandler(Exception.class) public String exceptionHandler(Exception e){ log.error("执行异常",e); if(e instanceof MethodArgumentNotValidException){ MethodArgumentNotValidException exception= (MethodArgumentNotValidException) e; return exception.getBindingResult().getFieldError().getDefaultMessage(); } else if(e instanceof EmosException) { EmosException exception = (EmosException) e; return exception.getMsg(); } else if (e instanceof IllegalStateException){ return "数据类型不正确"; } else if(e instanceof UnauthorizedException){ return "你不具备相关权限"; } else{ return "后端执行异常"; } } }
Service层使用异常类:
public int register(UserInfo req){ try { userMapper.register(req); }catch(BindingException e){ throw new EmosException("返回为空"); } return userMapper.register(req); }
注意⚠️:
如果没有写:
throw new EmosException("返回为空");
那么会自动执行全局捕获异常
Warning: Undefined variable $aria_req in /www/wwwroot/l.lvovl.cn/wp-content/themes/JieStyle-Two-master/comments.php on line 26
Warning: Undefined variable $aria_req in /www/wwwroot/l.lvovl.cn/wp-content/themes/JieStyle-Two-master/comments.php on line 27
Warning: Undefined variable $aria_req in /www/wwwroot/l.lvovl.cn/wp-content/themes/JieStyle-Two-master/comments.php on line 28