SpringBoot异常处理(一)
2024-09-08 03:20:27
ERROR:严重问题,我们无法处理
EXCEPTION:RuntimeException 编译期不检查,出现问题需要我们修改代码
非RuntimeException(CheckedException无这个异常类) 编译期必须处理,否则程序无法编译
自定义异常继承Excepition默认为非RuntimeExcepition。
异常 可以处理 用非RuntimeException 举例:处理文件,文件路径错误。
无能为力 RuntimeException 举例:数据库请求数据,数据库无此数据。
在编写后台时,为了方便前端快速定位错误,仅仅使用java自带的异常是不够的。需要自定义处理异常。
为了便于显示,本文统一错误响应格式:
//统一错误响应
{
code:10001
message:xxxx
request:GET url
}
UnifyResponse.class
@Getter
@Setter
@AllArgsConstructor
public class UnifyResponse {
private int code;
private String message;
private String request;
}
本文将异常分为两种:
未知异常:对于前端开发者和用户 都是无意义。通常是服务端开发者代码逻辑问题。
不需要将详细信息返还给前端和用户,可以统一显示服务器内部错误
已知异常
定义已知异常:
HttpException.class
public class HttpException extends RuntimeException {
protected Integer code;
protected Integer httpStatusCode = 500;
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public Integer getHttpStatusCode() {
return httpStatusCode;
}
public void setHttpStatusCode(Integer httpStatusCode) {
this.httpStatusCode = httpStatusCode;
}
}
NotFoundExcepiton.class
public class NotFoundException extends HttpException {
public NotFoundException(int code){
this.httpStatusCode = 404;
this.code = code;
}
}
处理异常:
GlobalExceptionAdvice.class:
@ControllerAdvice
public class GlobalExceptionAdvice {
@Autowired
private ExceptionCodeConfiguration exceptionCodeConfiguration;
//未知异常 错误码就直接硬编码为9999
@ResponseBody
@ExceptionHandler(value = Exception.class)
//如果不加这个注解,传回前端的http状态码会显示200 ok
@ResponseStatus(code = HttpStatus.INTERNAL_SERVER_ERROR)
public UnifyResponse handleException(HttpServletRequest req, Exception e) {
UnifyResponse message = new UnifyResponse(9999,"服务器异常",req.getMethod() + ' ' + req.getRequestURI());
//未知异常方便调试,产品上线可以去掉
System.out.println(e);
return message;
}
//已知异常 Http异常其中包含多种异常 错误码由程序员传入例如: throw new NotFoundException(10001);
//为什么不和上面未知异常一样使用@ResponseHandler 因为已知异常有很多种,状态码不一样,不能直接硬编码
//解决办法:在处理各种已知异常的类中添加http状态码,在返回的response中自己添加。使用ResponseEntity
@ExceptionHandler(HttpException.class)
public ResponseEntity<UnifyResponse> handleHttpException(HttpServletRequest req, HttpException e) {
UnifyResponse message = new UnifyResponse(e.getCode(),exceptionCodeConfiguration.getMessage(e.getCode()),req.getMethod() + ' ' + req.getRequestURI());
HttpHeaders httpHeaders = new HttpHeaders();
HttpStatus httpStatus = HttpStatus.resolve(e.getHttpStatusCode());
ResponseEntity<UnifyResponse> r = new ResponseEntity<>(message,httpHeaders,httpStatus);
return r;
}
}
配置类
已知异常的错误码和信息不要硬编码在异常类中,要善用properties配置类
exception-code.properties
liu.codes[0] = ok
liu.codes[999] = 服务器未知异常
liu.codes[10000] = 通用错误
liu.codes[10001] = 通用参数错误
liu.codes[10002] = 资源未找到
liu.codes[10003] = 没有找到合适的登陆处理方法
liu.codes[10004] = 令牌不合法或者过期
liu.codes[10005] = 用户未被授权
liu.codes[10006] = 登陆失败
liu.codes[20000] = 用户类通用错误
liu.codes[20001] = 用户已存在
liu.codes[20002] = 用户不存在
liu.codes[20003] = 用户密码错误
liu.codes[20004] = 获取用户wx openid失败
liu.codes[30000] = 商品类通用错误
liu.codes[30001] = 分类不存在
liu.codes[30002] = 商品信息不存
liu.codes[30003] = 主题不存在
liu.codes[30004] = complexity参数错误
liu.codes[30005] = Banner类资源不存在
liu.codes[30006] = 当前暂不支持级联获取sku-list
liu.codes[30007] = 当前暂不支持级联获取spu-list,请调用其他接口
liu.codes[30008] = 当前没有更多的商品信息了
liu.codes[30009] = Grid分类不存在
liu.codes[30010] = 请求的单品列表不存在
liu.codes[30011] = 请求的商品SaleExplain不存在
读取配置类:
ExceptionCodeConfiguration.class
@ConfigurationProperties(prefix = "liu")
@PropertySource(value = "classpath:/config/exception-code.properties")
@Component
public class ExceptionCodeConfiguration {
private Map<Integer,String> codes =new HashMap<>();
public Map<Integer, String> getCodes() {
return codes;
}
public void setCodes(Map<Integer, String> codes) {
this.codes = codes;
}
public String getMessage(int code){
return codes.get(code);
}
}
解决读取properties中文乱码
idea:
- File->setting->File Encoding 将Properties Files改为UTF-8编码,并将Transparent native-to-ascii 打勾。
- 可能上述操作后properties文件全部乱码,把内容删除在重新输入就可以了。
最新文章
- UIScrollView无法滚动的解决办法
- &ldquo;System.Data.SqlClient.SqlConnection&rdquo;的类型初始值设定项引发异常---解决方案
- [bzoj1071]组队[单调指针乱搞]
- ifrog-1028 Bob and Alice are playing numbers(trie树)
- fill的用法
- 白帽子讲Web安全2.pdf
- 前端bug记录---不定时更新
- 一个证书多次使用-导出p12文件
- 基于hadoop的图书推荐
- WebBrowser Cookie
- Spring 事务与脏读、不可重复读、幻读
- sql的优化
- 第52节:String,权限修饰符,方法,集合
- Dockerfile编写的注意事项
- 源码|ThreadLocal的实现原理
- css样式占位和不占位隐藏元素的方法
- Java多线程实现异步调用
- Docker 命令总结
- golang应用打包成docker镜像
- Linux网卡高级命令
热门文章
- Spring Boot + JPA 多模块项目无法注入 JpaRepository 接口
- 无需付费,教你IDEA社区版中开发Web项目(SpringBoot\Tomcat)
- 在Python实现print标准输出sys.stdout、stderr重定向及捕获的简单办法
- PyQt(Python+Qt)学习随笔:Designer中PushButton按钮default、atuoDefault属性
- C++中对一个布尔类型的变量按位取反结果不变
- <;阿里工程师的自我素养>;读后感-技术人应该具备的一些基本素质
- 十. Axios网络请求封装
- 打造云原生大型分布式监控系统(四): Kvass+Thanos 监控超大规模容器集群
- 水星路由器自动更换IP工具
- 数据结构—— Trie (前缀树)