电商项目专题(五)-通用的的异常处理
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 */DataNoArgsConstructorAllArgsConstructorpublicclassItem{privateInteger id;privateString name;privateLong price;}servicepackagecom.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){// 商品新增intidnewRandom().nextInt(100);item.setId(id);returnitem;}}controllerpackagecom.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 */RestControllerRequestMapping(item)publicclassItemController{AutowiredprivateItemService itemService;PostMappingpublicResponseEntityItemsaveItem(Item item){// 校验商品的价格if(item.getPrice()null){returnResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);}Item item1itemService.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 */RestControllerRequestMapping(item)publicclassItemController{AutowiredprivateItemService itemService;PostMappingpublicResponseEntityItemsaveItem(Item item){// 校验商品的价格if(item.getPrice()null){thrownewRuntimeException(价格不能为空);}Item item1itemService.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 */GetterNoArgsConstructorAllArgsConstructorpublicenumExceptionEnum{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.statusem.getCode();this.messageem.getMsg();this.timestampSystem.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 */NoArgsConstructorAllArgsConstructorGetterpublicclassYigouExceptionextendsRuntimeException{privateExceptionEnum exceptionEnum;}CommonExceptionHandler定义 通用的异常处理添加依赖不需要指定版本dependencygroupIdorg.springframework/groupIdartifactIdspring-webmvc/artifactId/dependencypackagecom.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)publicResponseEntityExceptionResulthandleException(YigouException e){returnResponseEntity.status(e.getExceptionEnum().getCode()).body(newExceptionResult(e.getExceptionEnum()));}}在yigou-item-service里面依赖yigou-commondependencygroupIdcom.yigou.parent/groupIdartifactIdyigou-common/artifactIdversion1.0-SNAPSHOT/version/dependency改写 ItemControllerpackagecom.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 */RestControllerRequestMapping(item)publicclassItemController{AutowiredprivateItemService itemService;PostMappingpublicResponseEntityItemsaveItem(Item item){// 校验商品的价格if(item.getPrice()null){thrownewYigouException(ExceptionEnum.PRICE_CANNOT_BE_NULL);}Item item1itemService.saveItem(item);returnResponseEntity.status(HttpStatus.CREATED).body(item1);}}测试

相关新闻

Python量化交易利器:pyctp CTP接口封装技术全解析

Python量化交易利器:pyctp CTP接口封装技术全解析

Python量化交易利器:pyctp CTP接口封装技术全解析 【免费下载链接】pyctp ctp wrapper for python 项目地址: https://gitcode.com/gh_mirrors/pyc/pyctp 在当今快速发展的金融科技领域,Python量化交易已经成为投资机构和个人投资者的重要工具。作…

2026/7/28 14:25:12阅读更多 →
TestDisk数据恢复:免费开源工具拯救丢失数据的完整指南

TestDisk数据恢复:免费开源工具拯救丢失数据的完整指南

TestDisk数据恢复:免费开源工具拯救丢失数据的完整指南 【免费下载链接】testdisk TestDisk & PhotoRec 项目地址: https://gitcode.com/gh_mirrors/te/testdisk 数据丢失是每个电脑用户的噩梦,但有了TestDisk这款免费开源的数据恢复工具&…

2026/7/28 14:25:12阅读更多 →
STM32与NBM7100A构建超低功耗物联网电源管理系统

STM32与NBM7100A构建超低功耗物联网电源管理系统

1. 项目背景与核心挑战在物联网和便携式设备领域,初级电池(不可充电电池)供电的设备面临着严峻的续航挑战。这类设备通常部署在难以维护或长期无人值守的环境中,比如远程传感器、智能仪表和紧急定位装置。我曾参与过一个农业环境监…

2026/7/28 14:25:12阅读更多 →
Claude Code与Codex深度对比:AI编程助手选型与实战指南

Claude Code与Codex深度对比:AI编程助手选型与实战指南

在AI编程助手领域,Claude Code和Codex无疑是当前最受瞩目的两个顶级选择。许多开发者在决定将哪个工具纳入自己的日常开发流时,常常陷入纠结:一个以强大的上下文处理和长会话记忆著称,另一个则以稳定的表现、高效的云任务委托和更…

2026/7/28 15:41:37阅读更多 →
游泰安大佛寺

游泰安大佛寺

从这里看,泰山非常雄伟。注意这并不是主峰。大佛寺门口。龙华殿,弥勒菩萨。牵牛花:

2026/7/28 15:41:37阅读更多 →
每日安全情报报告 · 2026-07-28

每日安全情报报告 · 2026-07-28

每日安全情报报告 2026-07-28 由 AI 整理发布 一、最新高危漏洞 1. CVE-2026-61511 — vBulletin 预认证远程代码执行 ⚠️ PoC 已公开 项目详情CVE 编号CVE-2026-61511漏洞类型代码注入(CWE-94,PHP eval() 注入)受影响组件vBulletin 6.x&…

2026/7/28 15:41:37阅读更多 →
企业Agent走向业务深处:规模化落地的三道关口

企业Agent走向业务深处:规模化落地的三道关口

企业Agent正面临规模化落地挑战。麦肯锡2026年披露,全球62%的企业正在试验AI Agent,仅23%真正实现规模化部署。国内市场也处在相似的转折点。根据爱分析调研,中国56%的企业处于试点速赢阶段,只有13%进入全面推广阶段。企业管理层的…

2026/7/28 15:41:37阅读更多 →
File-Based App开发:轻量级数据存储与MVP快速验证

File-Based App开发:轻量级数据存储与MVP快速验证

1. 项目背景与核心价值 File-Based App开发模式正在成为快速验证产品原型的利器。这种开发方式的核心在于将应用数据以文件形式存储在本地,而非依赖复杂的数据库系统。我在最近的一个电商类MVP项目中,仅用3天就完成了核心功能的开发和演示,这…

2026/7/28 15:41:37阅读更多 →
linux中用户栈与内核栈有什么区别

linux中用户栈与内核栈有什么区别

内核在创建进程时,会同时创建task_struct和进程相应堆栈。每个进程都会有两个堆栈,一个用户栈,存在于用户空间,一个内核栈,存在于内核空间。当进程在用户空间运行时,CPU堆栈寄存器的内容是用户堆栈地址&…

2026/7/28 15:39:37阅读更多 →
覆盖国产 + 海外 + 开源模型,OpenClaw 2.7.9 Windows/Mac 双端部署详解

覆盖国产 + 海外 + 开源模型,OpenClaw 2.7.9 Windows/Mac 双端部署详解

🔹 工具基础介绍 OpenClaw 是开源生态中一款实用性较强的本地智能工具,凭借本地离线运行、可视化图形操作和任务自动化三大核心特性,赢得了众多用户的青睐。与普通在线对话AI工具不同,它属于能够直接操控本机软硬件的智能数字员工…

2026/7/28 4:06:39阅读更多 →
伺服阀焊完微漏毁整机?精密激光焊接三关锁住高压

伺服阀焊完微漏毁整机?精密激光焊接三关锁住高压

所谓液压伺服阀体的精密激光焊接,是用激光束对阀座壳体(通常为不锈钢或铝合金)进行密封焊接,使阀体在21-35MPa的高压液压油或压缩气体中长期运行而不发生介质泄漏。液压伺服阀是高端液压系统的"大脑"。从航空航天飞行控…

2026/7/28 2:08:06阅读更多 →
D2DX:三步实现《暗黑破坏神2》高清宽屏体验的终极指南

D2DX:三步实现《暗黑破坏神2》高清宽屏体验的终极指南

D2DX:三步实现《暗黑破坏神2》高清宽屏体验的终极指南 【免费下载链接】d2dx D2DX is a complete solution to make Diablo II run well on modern PCs, with high fps and better resolutions. 项目地址: https://gitcode.com/gh_mirrors/d2/d2dx 你是否还在…

2026/7/28 1:38:28阅读更多 →
告别臃肿!3步让你的暗影精灵笔记本重获新生

告别臃肿!3步让你的暗影精灵笔记本重获新生

告别臃肿!3步让你的暗影精灵笔记本重获新生 【免费下载链接】OmenSuperHub Control Omen laptop performance, fan speeds, and keyboard lighting, and unlock power limits. 项目地址: https://gitcode.com/gh_mirrors/om/OmenSuperHub 你是否也曾为官方Om…

2026/7/28 0:00:29阅读更多 →
RAG必踩坑!财报法规检索不准?这款开源工具让答案浮出水面,准确率飙升98.7%!

RAG必踩坑!财报法规检索不准?这款开源工具让答案浮出水面,准确率飙升98.7%!

做 RAG 的人应该都踩过这个致命的坑:把几百页的财报、法规、技术手册扔给向量库,问一个具体问题,搜出来的全是沾边但没用的内容 —— 关键信息要么被硬切块拆碎了,要么藏在几十条结果的最下面。语义相似≠真正相关,这个…

2026/7/28 0:00:29阅读更多 →
抖音视频文案提取工具全指南:免费2026版、手机App、在线工具一网打尽

抖音视频文案提取工具全指南:免费2026版、手机App、在线工具一网打尽

2026年做短视频运营,从抖音上扒文案早就不是偷偷抄笔记的事了。我刚开始做内容的时候,每天刷半小时抖音,手动把爆款视频的口播敲进备忘录,一条2分钟的视频得花十来分钟,碰到语速快的还要反复回听。后来试了一圈工具&am…

2026/7/28 0:00:29阅读更多 →
YOLOv8推理性能优化:从1.2FPS到35FPS的全链路加速实践

YOLOv8推理性能优化:从1.2FPS到35FPS的全链路加速实践

如果你在部署 YOLOv8 时,发现推理速度只有可怜的 1-2 FPS,而别人的演示视频却能跑到 30 FPS 以上,那么问题很可能不在模型本身,而在于你的整个处理链路。很多开发者拿到一个训练好的 YOLOv8 模型后,会直接使用官方示例…

2026/7/27 16:57:54阅读更多 →
Coze与Dify对比指南:低代码AI应用开发从入门到实战

Coze与Dify对比指南:低代码AI应用开发从入门到实战

1. 从零到一:为什么你需要了解 Coze 和 Dify?如果你对 AI 应用开发感兴趣,但一看到“大模型”、“智能体”、“工作流”这些词就头疼,觉得门槛太高,那这篇文章就是为你准备的。很多开发者,包括我自己&#…

2026/7/28 3:17:03阅读更多 →
AI生图工具怎么选?2026年6月版实测对比

AI生图工具怎么选?2026年6月版实测对比

做自媒体的朋友应该都有体会:配图一直是个让人头疼的问题。2026年,AI生图工具已经非常成熟了,但工具太多反而不知道怎么选。以下是截至2026年6月我对主流AI生图工具的实测对比。Midjourney V8.1:速度之王2026年6月11日&#xff0c…

2026/7/28 2:35:58阅读更多 →