电商项目专题(五)-通用的的异常处理
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);}}测试

相关新闻

3大核心功能揭秘:XCOM 2模组管理器AML如何让你的游戏体验提升300%

3大核心功能揭秘:XCOM 2模组管理器AML如何让你的游戏体验提升300%

3大核心功能揭秘:XCOM 2模组管理器AML如何让你的游戏体验提升300% 【免费下载链接】xcom2-launcher The Alternative Mod Launcher (AML) is a replacement for the default game launchers from XCOM 2 and XCOM Chimera Squad. 项目地址: https://gitcode.com/g…

2026/7/28 14:26:13 阅读更多 →
京东自动化脚本终极指南:3步实现24小时无人值守收益管理

京东自动化脚本终极指南:3步实现24小时无人值守收益管理

京东自动化脚本终极指南:3步实现24小时无人值守收益管理 【免费下载链接】jd_scripts-lxk0301 长期活动,自用为主 | 低调使用,请勿到处宣传 | 备份lxk0301的源码仓库 项目地址: https://gitcode.com/gh_mirrors/jd/jd_scripts-lxk0301 …

2026/7/28 14:26:13 阅读更多 →
应用上传到谷歌商店

应用上传到谷歌商店

参考链接 https://blog.csdn.net/cjh94520/article/details/70306601

2026/7/28 14:26:13 阅读更多 →

最新新闻

计算机毕业设计之“桃夭”汉服商城的设计与实现

计算机毕业设计之“桃夭”汉服商城的设计与实现

由于移动应用技术的持续性的快速发展,现实生活中人们大多数都是通过移动手机、电脑等智能设备来完成生活中的事务。因此,许多的人工传统行业也开始与互联网结合,不再一味的依靠人工手动,努力打造半自动数字化甚至是全自动数字化模…

2026/7/28 14:39:18 阅读更多 →
Steam创意工坊下载困境:非Steam玩家的终极解决方案

Steam创意工坊下载困境:非Steam玩家的终极解决方案

Steam创意工坊下载困境:非Steam玩家的终极解决方案 【免费下载链接】WorkshopDL WorkshopDL - The Best Steam Workshop Downloader 项目地址: https://gitcode.com/gh_mirrors/wo/WorkshopDL 你是否在Epic Games Store或GOG平台购买了心仪的游戏&#xff0c…

2026/7/28 14:39:18 阅读更多 →
从机械传动到3D打印:创客如何打造“机械狐狸”自动装置

从机械传动到3D打印:创客如何打造“机械狐狸”自动装置

1. 项目概述:当机械美学遇上创客精神最近在创客圈子里,一个融合了古典机械美学与现代开源硬件的项目——“机械狐狸”,搭配上极具辨识度的“苹果机械表”风格设计,引发了不少讨论。这听起来像是一个天马行空的跨界构想&#xff0c…

2026/7/28 14:39:18 阅读更多 →
LTE Cat 1与STM32F303K8在工业物联网中的通信方案

LTE Cat 1与STM32F303K8在工业物联网中的通信方案

1. 项目背景与核心组件解析在工业物联网领域,稳定可靠的通信连接是系统设计的核心挑战。LARA-R6401D-00B作为u-blox推出的LTE Cat 1蜂窝通信模块,与STM32F303K8微控制器的组合,为需要中等带宽、实时控制和严格安全要求的应用场景提供了理想的…

2026/7/28 14:39:18 阅读更多 →
VSWR与反射系数Γ的关系解析及工程应用

VSWR与反射系数Γ的关系解析及工程应用

1. 射频工程师的日常困惑:VSWR和Γ到底什么关系?作为一名在射频领域摸爬滚打多年的工程师,我至今记得刚入行时被各种参数支配的恐惧。其中最让人头疼的就是VSWR(电压驻波比)和Γ(反射系数)这对&…

2026/7/28 14:39:18 阅读更多 →
架构师必备:分布式锁方案选型

架构师必备:分布式锁方案选型

当同时操作共享资源时,需要做并发控制。在单机上用synchronized或ReentrantLock能解决的互斥问题,到了多机部署的分布式环境就行不通了,因为它们只在单个JVM进程内生效,跨多机就需要分布式锁了。 先说结论,最常用的分…

2026/7/28 14:38:17 阅读更多 →

日新闻

告别臃肿!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:43 阅读更多 →
RAG必踩坑!财报法规检索不准?这款开源工具让答案浮出水面,准确率飙升98.7%!

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

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

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

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

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

2026/7/28 0:00:43 阅读更多 →

周新闻

深度学习道路桥梁裂缝检测系统 道路桥梁裂缝检测数据集 道路桥梁病害识别检测数据集

深度学习道路桥梁裂缝检测系统 道路桥梁裂缝检测数据集 道路桥梁病害识别检测数据集

深度学习道路桥梁裂缝检测系统 数据集6000张 完整源码已标注数据集训练好的模型环境配置教程程序运行说明文档,可以直接使用!系统支持图片、视频、摄像头等多种方式检测裂缝,功能强大实用。 1数据集6000张 8各类别

2026/7/28 12:04:22 阅读更多 →
深度学习YOLO模型如何训练 PUBG 绝地求生目标检测数据集

深度学习YOLO模型如何训练 PUBG 绝地求生目标检测数据集

pubg数据集 精选原图1.42万数据 1.49万标签 无任何重复、算法增强或冗余图像! pubg绝地求生目标检测数据集 1分类:e_body,14905个标签,txt格式 共计14244张图,99%为640*640尺寸图像 适合yolo目标检测、AI训练关键词&am…

2026/7/28 8:29:16 阅读更多 →
Apex英雄目标检测数据集 深度学习框架YOLO如何训练APEX数据集

Apex英雄目标检测数据集 深度学习框架YOLO如何训练APEX数据集

Apex检测数据集数据集详情检测类别: allies enemy tag图片总量:7247张训练集:5139张验证集:1425张测试集:683张标注状态:全部已标注,即拿即用数据格式:支持YOLO格式及其他格式&#…

2026/7/28 5:03:42 阅读更多 →

月新闻