SpringBoot封装通用异常
1.添加依赖
1 2 3 4 5 | <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version> 5.0 ..RELEASE</version> </dependency> |
2.自定义异常类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | /** *自定义异常类 */ public class CommonException extends RuntimeException { private ExceptionEnum exceptionEunm; public CommonException(ExceptionEnum exceptionEunm) { this .exceptionEunm = exceptionEunm; } public ExceptionEnum getExceptionEunm() { return exceptionEunm; } public void setExceptionEunm(ExceptionEnum exceptionEunm) { this .exceptionEunm = exceptionEunm; } } |
3.自定义异常类型的枚举类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | ** * 自定义异常类型的枚举类 */ public enum ExceptionEnum { PRICE_CANNOT_NULL( 400 , "价格不能为空" ), ; private final Integer code; private final String msg; ExceptionEnum(Integer code, String msg) { this .code = code; this .msg = msg; } public Integer getCode() { return code; } public String getMsg() { return msg; } } |
4.自定义异常返回数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | /** * 自定义异常返回数据 */ public class ExceptionResult { private Integer status; private String message; private Long timestamp; public ExceptionResult(ExceptionEnum exceptionEnum){ this .status= exceptionEnum.getCode(); this .message=exceptionEnum.getMsg(); this .timestamp = System.currentTimeMillis(); } public Integer getStatus() { return status; } public void setStatus(Integer status) { this .status = status; } public String getMessage() { return message; } public void setMessage(String message) { this .message = message; } public Long getTimestamp() { return timestamp; } public void setTimestamp(Long timestamp) { this .timestamp = timestamp; } @Override public String toString() { return "ExceptionResult{" + "status=" + status + ", message='" + message + '\ '' + ", timestamp=" + timestamp + '}' ; } } |
5.通用异常通知
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | ** * 通用异常通知 * @ControllerAdvice :默认会自动拦截所有的Controller */ @ControllerAdvice public class CommonExceptionHandler { @ExceptionHandler (CommonException. class ) //拦截指定抛出的自定义CommonException异常 public ResponseEntity<ExceptionResult> handlerException(CommonException e){ return ResponseEntity.status(e.getExceptionEunm().getCode()) .body( new ExceptionResult(e.getExceptionEunm())); } } |
6.实体类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | public class Item { private Integer id; private String name; private Long price; public Integer getId() { return id; } public void setId(Integer id) { this .id = id; } public String getName() { return name; } public void setName(String name) { this .name = name; } public Long getPrice() { return price; } public void setPrice(Long price) { this .price = price; } @Override public String toString() { return "Item{" + "id=" + id + ", name='" + name + '\ '' + ", price=" + price + '}' ; } } |
7.写一个ItemController类测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | @RestController @RequestMapping ( "/item" ) public class ItemController { @Autowired private ItemService itemService; @PostMapping () public ResponseEntity<Item> saveItem(Item item){ if (item.getPrice() == null ){ throw new CommonException(ExceptionEnum.PRICE_CANNOT_NULL); } Item item1 = itemService.saveItem(item); return ResponseEntity.status(HttpStatus.CREATED).body(item1); } } |
8.测试结果
成功返回
自定义异常返回
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南