
正文
Spring Boot 构建电商基础秒杀项目 (三) 通用的返回对象 & 异常处理
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
SpringBoot构建电商基础秒杀项目 学习笔记
定义通用的返回对象
public class CommonReturnType { // success, fail
private String status; // status = success, data 内返回前端需要的 json数据
// status = fail, data 内使用通用的错误码格式
private Object data; public static CommonReturnType create(Object result){ return create(result, "success");
} public static CommonReturnType create(Object result, String status){ CommonReturnType type = new CommonReturnType();
type.setStatus(status);
type.setData(result); return type;
} public String getStatus() {
return status;
} public void setStatus(String status) {
this.status = status;
} public Object getData() {
return data;
} public void setData(Object data) {
this.data = data;
}
}
定义错误接口
public interface CommonError { public int getErrCode();
public String getErrMsg();
public CommonError setErrMsg(String errMsg);
}
定义错误类型枚举
public enum EmBusinessError implements CommonError { // 通用错误类型 10001
PARAMETER_VALIDATION_ERROR(10001, "参数不合法"),
UNKNOWN_ERROR(10002, "未知错误"), // 20000 开头为用户信息相关错误定义
USER_NOT_EXIST(20001, "用户不存在"), ; private int errCode;
private String errMsg; private EmBusinessError(int errCode, String errMsg){
this.errCode = errCode;
this.errMsg = errMsg;
} @Override
public int getErrCode() {
return this.errCode;
} @Override
public String getErrMsg() {
return this.errMsg;
} @Override
public CommonError setErrMsg(String errMsg) {
this.errMsg = errMsg;
return this;
}
}
包装器业务异常类实现
// 包装器业务异常类实现
public class BusinessException extends Exception implements CommonError { private CommonError commonError; public BusinessException(CommonError commonError){
super();
this.commonError = commonError;
} public BusinessException(CommonError commonError, String errMsg){
super();
this.commonError = commonError;
this.commonError.setErrMsg(errMsg);
} @Override
public int getErrCode() {
return this.commonError.getErrCode();
} @Override
public String getErrMsg() {
return this.commonError.getErrMsg();
} @Override
public CommonError setErrMsg(String errMsg) {
this.commonError.setErrMsg(errMsg);
return this;
}
}
定义 BaseController 处理异常
public class BaseController { // 定义 exceptionhandler 解决未被 controller 层吸收的 exception
@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public Object handlerException(HttpServletRequest request, Exception ex){ Map<String, Object> responseData = new HashMap<>(); if(ex instanceof BusinessException){
BusinessException businessException = (BusinessException)ex;
responseData.put("errCode", businessException.getErrCode());
responseData.put("errMsg", businessException.getErrMsg());
}else{
responseData.put("errCode", EmBusinessError.UNKNOWN_ERROR.getErrCode());
responseData.put("errMsg", EmBusinessError.UNKNOWN_ERROR.getErrMsg());
} return CommonReturnType.create(responseData, "fail");
}
}
修改 UserController
@Controller("user")
@RequestMapping("/user")
public class UserController extends BaseController{ @Autowired
private UserService userService; @RequestMapping("/get")
@ResponseBody
public CommonReturnType getUser(@RequestParam(name="id") Integer id) throws BusinessException{
UserModel userModel = userService.getUserById(id); if(userModel == null){
// userModel.setEncrptPassword("123");
throw new BusinessException(EmBusinessError.USER_NOT_EXIST);
} UserVO userVO = convertFromModel(userModel); return CommonReturnType.create(userVO);
} private UserVO convertFromModel(UserModel userModel){
if(userModel == null){
return null;
} UserVO userVO = new UserVO();
BeanUtils.copyProperties(userModel, userVO); return userVO;
}
}
源码:spring-boot-seckill
Spring Boot 构建电商基础秒杀项目 (三) 通用的返回对象 & 异常处理的更多相关文章- Spring Boot 构建电商基础秒杀项目 (一) 项目搭建
SpringBoot构建电商基础秒杀项目 学习笔记 Spring Boot 其实不是什么新的框架,它默认配置了很多框架的使用方式,就像 maven 整合了所有的 jar 包, Spring Boot ...
- Spring Boot 构建电商基础秒杀项目 (十二) 总结 (完结)
SpringBoot构建电商基础秒杀项目 学习笔记 系统架构 存在问题 如何发现容量问题 如何使得系统水平扩展 查询效率低下 活动开始前页面被疯狂刷新 库存行锁问题 下单操作步骤多,缓慢 浪涌流量如何 ...
- Spring Boot 构建电商基础秒杀项目 (十一) 秒杀
SpringBoot构建电商基础秒杀项目 学习笔记 新建表 create table if not exists promo ( id int not null auto_increment, pro ...
- Spring Boot 构建电商基础秒杀项目 (十) 交易下单
SpringBoot构建电商基础秒杀项目 学习笔记 新建表 create table if not exists order_info ( id varchar(32) not null defaul ...
- Spring Boot 构建电商基础秒杀项目 (九) 商品列表 &; 详情
SpringBoot构建电商基础秒杀项目 学习笔记 ItemDOMapper.xml 添加 <select id="listItem" resultMap="Bas ...
- Spring Boot 构建电商基础秒杀项目 (八) 商品创建
SpringBoot构建电商基础秒杀项目 学习笔记 新建数据表 create table if not exists item ( id int not null auto_increment, ti ...
- Spring Boot 构建电商基础秒杀项目 (七) 自动校验
SpringBoot构建电商基础秒杀项目 学习笔记 修改 UserModel 添加注解 public class UserModel { private Integer id; @NotBlank(m ...
- Spring Boot 构建电商基础秒杀项目 (六) 用户登陆
SpringBoot构建电商基础秒杀项目 学习笔记 userDOMapper.xml 添加 <select id="selectByTelphone" resultMap=& ...
- Spring Boot 构建电商基础秒杀项目 (五) 用户注册
SpringBoot构建电商基础秒杀项目 学习笔记 UserService 添加 void register(UserModel userModel) throws BusinessException ...
随机推荐- display:block 不起作用
jquery中$("#Main").css("display","none"); $("#Day").css('disp ...
- [HIHO1062] 最近公共祖先&#183;一(lca, 并查集, 二分, 神trick)
题目链接:http://hihocoder.com/problemset/problem/1062 题意裸,有个trick,导致我当年做的时候一直在WA... 那就是出现这种没有出现在关系中,但是依然 ...
- struts2拦截器与过滤器
转载:http://www.cnblogs.com/JohnLiang/archive/2011/12/15/2288376.html 过滤器,是在java web中,你传入的request,resp ...
- PHP 自 5.2 到 5.6 中新增的功能详解
截至目前(2014.2), PHP 的最新稳定版本是 PHP5.5, 但有差不多一半的用户仍在使用已经不在维护 [注] 的 PHP5.2, 其余的一半用户在使用 PHP5.3 [注].因为 PHP 那 ...
- ORACLE基本SQL语句-添加更新数据函数篇
一.添加数据 /*添加数据*/insert into STU values('stu0004','赵一',18,1,"kc0004");insert into STU(STU_ID ...
- tcpdump 抓包让wireshark来分析
在linux下面用tcpdump 抓包非常方便, 但是抓的包要提取出来进行分析, 还是得用wireshark来过滤分析比较方便. 下面先介绍一下 TCPDUMP 的使用 例:tcpdump host ...
- C++构造函数(二)
本篇是介绍C++的构造函数的第二篇(共二篇),属于读书笔记,对C++进行一个系统的复习. 复制构造函数 复制构造函数是构造函数的一种,也被称为拷贝构造函数,他只有一个参数,参数类型是本类的引用.默认构 ...
- bashell基础
身为一个iOS程序员,虽然iOS相关技术十分重要,但是bash也是不可不了解的,因为技能的成长,除了深度,还需要广度.下面就来介绍下bash. Shell是C语言编写的,所以他是解释性语言,运行在Li ...
- Solr之java操作
参考教程: http://www.cnblogs.com/xia520pi/p/3625232.html http://www.cnblogs.com/hujunzheng/p/5647896.htm ...
- hive函数 parse_url的使用
hive提供了直接处理url的函数 parse_url desc funtion 的解释是: parse_url(url, partToExtract[, key]) - extracts a par ...
SpringBoot构建电商基础秒杀项目 学习笔记 Spring Boot 其实不是什么新的框架,它默认配置了很多框架的使用方式,就像 maven 整合了所有的 jar 包, Spring Boot ...
SpringBoot构建电商基础秒杀项目 学习笔记 系统架构 存在问题 如何发现容量问题 如何使得系统水平扩展 查询效率低下 活动开始前页面被疯狂刷新 库存行锁问题 下单操作步骤多,缓慢 浪涌流量如何 ...
SpringBoot构建电商基础秒杀项目 学习笔记 新建表 create table if not exists promo ( id int not null auto_increment, pro ...
SpringBoot构建电商基础秒杀项目 学习笔记 新建表 create table if not exists order_info ( id varchar(32) not null defaul ...
SpringBoot构建电商基础秒杀项目 学习笔记 ItemDOMapper.xml 添加 <select id="listItem" resultMap="Bas ...
SpringBoot构建电商基础秒杀项目 学习笔记 新建数据表 create table if not exists item ( id int not null auto_increment, ti ...
SpringBoot构建电商基础秒杀项目 学习笔记 修改 UserModel 添加注解 public class UserModel { private Integer id; @NotBlank(m ...
SpringBoot构建电商基础秒杀项目 学习笔记 userDOMapper.xml 添加 <select id="selectByTelphone" resultMap=& ...
SpringBoot构建电商基础秒杀项目 学习笔记 UserService 添加 void register(UserModel userModel) throws BusinessException ...
- display:block 不起作用
jquery中$("#Main").css("display","none"); $("#Day").css('disp ...
- [HIHO1062] 最近公共祖先&#183;一(lca, 并查集, 二分, 神trick)
题目链接:http://hihocoder.com/problemset/problem/1062 题意裸,有个trick,导致我当年做的时候一直在WA... 那就是出现这种没有出现在关系中,但是依然 ...
- struts2拦截器与过滤器
转载:http://www.cnblogs.com/JohnLiang/archive/2011/12/15/2288376.html 过滤器,是在java web中,你传入的request,resp ...
- PHP 自 5.2 到 5.6 中新增的功能详解
截至目前(2014.2), PHP 的最新稳定版本是 PHP5.5, 但有差不多一半的用户仍在使用已经不在维护 [注] 的 PHP5.2, 其余的一半用户在使用 PHP5.3 [注].因为 PHP 那 ...
- ORACLE基本SQL语句-添加更新数据函数篇
一.添加数据 /*添加数据*/insert into STU values('stu0004','赵一',18,1,"kc0004");insert into STU(STU_ID ...
- tcpdump 抓包让wireshark来分析
在linux下面用tcpdump 抓包非常方便, 但是抓的包要提取出来进行分析, 还是得用wireshark来过滤分析比较方便. 下面先介绍一下 TCPDUMP 的使用 例:tcpdump host ...
- C++构造函数(二)
本篇是介绍C++的构造函数的第二篇(共二篇),属于读书笔记,对C++进行一个系统的复习. 复制构造函数 复制构造函数是构造函数的一种,也被称为拷贝构造函数,他只有一个参数,参数类型是本类的引用.默认构 ...
- bashell基础
身为一个iOS程序员,虽然iOS相关技术十分重要,但是bash也是不可不了解的,因为技能的成长,除了深度,还需要广度.下面就来介绍下bash. Shell是C语言编写的,所以他是解释性语言,运行在Li ...
- Solr之java操作
参考教程: http://www.cnblogs.com/xia520pi/p/3625232.html http://www.cnblogs.com/hujunzheng/p/5647896.htm ...
- hive函数 parse_url的使用
hive提供了直接处理url的函数 parse_url desc funtion 的解释是: parse_url(url, partToExtract[, key]) - extracts a par ...






