news 2026/7/28 14:27:11

电商项目专题(五)-通用的的异常处理

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
电商项目专题(五)-通用的的异常处理

1.场景预设

1.1.场景

加入我们做新增商品,需要接收下面的参数:

price:价格 name:名称

然后对数据做简单校验:

价格不能为空

新增时,自动生成 ID ,然后随商品对象一起返回

1.2.代码实现

项目结构:

实体类:

packagecom.yigou.item.pojo;importlombok.AllArgsConstructor;importlombok.Data;importlombok.NoArgsConstructor;/** * @author bruceliu * @create 2019-09-01 12:36 * @description */@Data@NoArgsConstructor@AllArgsConstructorpublicclassItem{privateInteger id;privateString name;privateLong price;}

service:

packagecom.yigou.item.service;importcom.yigou.item.pojo.Item;importorg.springframework.stereotype.Service;importjava.util.Random;/** * @author bruceliu * @create 2019-09-03 22:12 * @description */@ServicepublicclassItemService{publicItemsaveItem(Item item){// 商品新增intid=newRandom().nextInt(100);item.setId(id);returnitem;}}

controller:

packagecom.yigou.item.controller;importcom.yigou.item.pojo.Item;importjava.util.List;importcom.yigou.item.service.ItemService;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.http.HttpStatus;importorg.springframework.http.ResponseEntity;importorg.springframework.web.bind.annotation.PostMapping;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RestController;/** * @author bruceliu * @create 2019-09-01 12:38 * @description */@RestController@RequestMapping("item")publicclassItemController{@AutowiredprivateItemService itemService;@PostMappingpublicResponseEntity<Item>saveItem(Item item){// 校验商品的价格if(item.getPrice()==null){returnResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);}Item item1=itemService.saveItem(item);returnResponseEntity.status(HttpStatus.CREATED).body(item1);}}

测试:

如果price为空:

改写Controller给出异常消息:

packagecom.yigou.item.controller;importcom.yigou.item.pojo.Item;importjava.util.List;importcom.yigou.item.service.ItemService;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.http.HttpStatus;importorg.springframework.http.ResponseEntity;importorg.springframework.web.bind.annotation.PostMapping;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RestController;/** * @author bruceliu * @create 2019-09-01 12:38 * @description */@RestController@RequestMapping("item")publicclassItemController{@AutowiredprivateItemService itemService;@PostMappingpublicResponseEntity<Item>saveItem(Item item){// 校验商品的价格if(item.getPrice()==null){thrownewRuntimeException("价格不能为空!");}Item item1=itemService.saveItem(item);returnResponseEntity.status(HttpStatus.CREATED).body(item1);}}

测试结果:

是SpringMvc处理的异常,但是500是服务器内部错误,而真正异常的原因是客户端引发的异常
上述异常是由 SpringMVC 处理的,而我们真正要的是自己处理!

2.通用的异常处理

放到yigou-common里面公共起来

ExceptionEnum:枚举 异常

packagecom.yigou.common.enums;importlombok.*;/** * @author bruceliu * @create 2019-09-03 22:35 * @description */@Getter@NoArgsConstructor@AllArgsConstructorpublicenumExceptionEnum{PRICE_CANNOT_BE_NULL(400,"价格不能为空!");privateintcode;privateString msg;}

ExceptionResult:异常结果 定义

packagecom.yigou.common.vo;importcom.yigou.common.enums.ExceptionEnum;importlombok.Data;/** * @author bruceliu * @create 2019-09-03 22:37 * @description */@DatapublicclassExceptionResult{privateintstatus;privateString message;privateLong timestamp;publicExceptionResult(ExceptionEnum em){this.status=em.getCode();this.message=em.getMsg();this.timestamp=System.currentTimeMillis();}}

YigouException:自定义 异常

packagecom.yigou.common.exception;importcom.yigou.common.enums.ExceptionEnum;importlombok.AllArgsConstructor;importlombok.Getter;importlombok.NoArgsConstructor;/** * @author bruceliu * @create 2019-09-03 22:40 * @description */@NoArgsConstructor@AllArgsConstructor@GetterpublicclassYigouExceptionextendsRuntimeException{privateExceptionEnum exceptionEnum;}

CommonExceptionHandler:定义 通用的异常处理
添加依赖(不需要指定版本)

<dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId></dependency>
packagecom.yigou.common.advice;importcom.yigou.common.exception.YigouException;importcom.yigou.common.vo.ExceptionResult;importorg.springframework.http.ResponseEntity;importorg.springframework.web.bind.annotation.ControllerAdvice;importorg.springframework.web.bind.annotation.ExceptionHandler;/** * @author bruceliu * @create 2019-09-03 22:41 * @description */@ControllerAdvicepublicclassCommonExceptionHandler{@ExceptionHandler(YigouException.class)publicResponseEntity<ExceptionResult>handleException(YigouException e){returnResponseEntity.status(e.getExceptionEnum().getCode()).body(newExceptionResult(e.getExceptionEnum()));}}

yigou-item-service里面依赖yigou-common

<dependency><groupId>com.yigou.parent</groupId><artifactId>yigou-common</artifactId><version>1.0-SNAPSHOT</version></dependency>

改写 ItemController

packagecom.yigou.item.controller;importcom.yigou.common.enums.ExceptionEnum;importcom.yigou.common.exception.YigouException;importcom.yigou.item.pojo.Item;importjava.util.List;importcom.yigou.item.service.ItemService;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.http.HttpStatus;importorg.springframework.http.ResponseEntity;importorg.springframework.web.bind.annotation.PostMapping;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RestController;/** * @author bruceliu * @create 2019-09-01 12:38 * @description */@RestController@RequestMapping("item")publicclassItemController{@AutowiredprivateItemService itemService;@PostMappingpublicResponseEntity<Item>saveItem(Item item){// 校验商品的价格if(item.getPrice()==null){thrownewYigouException(ExceptionEnum.PRICE_CANNOT_BE_NULL);}Item item1=itemService.saveItem(item);returnResponseEntity.status(HttpStatus.CREATED).body(item1);}}

测试:

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/28 14:27:00

C语言共用体

最近看Linux内核源码&#xff0c;看到很多地方有共用体&#xff0c;应为一般不使用它&#xff0c;一下子还忘了。留个链接记录一下。 https://blog.csdn.net/liubing8609/article/details/83150752

作者头像 李华
网站建设 2026/7/28 14:25:43

为什么我的 GraphRAG 上线就崩?把知识图谱和 RAG 结合起来后的真实踩坑

这篇不先堆名词。我们把《一次GraphRAG项目复盘&#xff0c;问题最后出在流程而不是模型》拆成几级台阶&#xff0c;看完至少知道下一步该学什么、该练什么。摘要摘要&#xff1a;传统 RAG 在小团队落地时往往在检索阶段“卡脖子”&#xff0c;本文结合一次实际项目复盘&#x…

作者头像 李华
网站建设 2026/7/28 14:25:39

Agent 踩坑实录:小团队如何平衡规划、工具调用与记忆?

聊《一次Agent项目复盘&#xff0c;问题最后出在流程而不是模型》之前&#xff0c;先说一句实在的&#xff1a;别急着背概念&#xff0c;先看它在真实项目里到底解决什么问题。摘要摘要&#xff1a;本文以 Java 技术那些事团队的真实项目为背景&#xff0c;探讨了在资源有限的情…

作者头像 李华
网站建设 2026/7/28 14:24:38

工业物联网设备低功耗电源管理方案设计与优化

1. 项目背景与核心挑战在工业物联网终端设备的设计中&#xff0c;电源管理系统往往决定着产品的成败。去年我在设计一款野外气象监测站时&#xff0c;就遇到了这样的困境&#xff1a;设备需要在-20℃~70℃环境中持续工作三年不更换电池&#xff0c;同时要为高精度传感器、LoRa无…

作者头像 李华
网站建设 2026/7/28 14:21:40

Windows 11专业版Docker与WSL2完整部署指南:从系统准备到AI开发环境调优

如果你在 Windows 11 上折腾 Docker 和 WSL2 时,反复遇到“无法更新”、“无法安装”或者装好了却用不了的问题,那这篇文章就是为你准备的。尤其是在 AI 开发、机器学习这类需要稳定容器环境的场景下,一个顺畅的 Docker 环境是基础中的基础。很多人会告诉你“用专业版”,但…

作者头像 李华