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 构建电商基础秒杀项目 (三) 通用的返回对象 & 异常处理的更多相关文章

  1. Spring Boot 构建电商基础秒杀项目 (一) 项目搭建

    SpringBoot构建电商基础秒杀项目 学习笔记 Spring Boot 其实不是什么新的框架,它默认配置了很多框架的使用方式,就像 maven 整合了所有的 jar 包, Spring Boot ...

  2. Spring Boot 构建电商基础秒杀项目 (十二) 总结 (完结)

    SpringBoot构建电商基础秒杀项目 学习笔记 系统架构 存在问题 如何发现容量问题 如何使得系统水平扩展 查询效率低下 活动开始前页面被疯狂刷新 库存行锁问题 下单操作步骤多,缓慢 浪涌流量如何 ...

  3. Spring Boot 构建电商基础秒杀项目 (十一) 秒杀

    SpringBoot构建电商基础秒杀项目 学习笔记 新建表 create table if not exists promo ( id int not null auto_increment, pro ...

  4. Spring Boot 构建电商基础秒杀项目 (十) 交易下单

    SpringBoot构建电商基础秒杀项目 学习笔记 新建表 create table if not exists order_info ( id varchar(32) not null defaul ...

  5. Spring Boot 构建电商基础秒杀项目 (九) 商品列表 & 详情

    SpringBoot构建电商基础秒杀项目 学习笔记 ItemDOMapper.xml 添加 <select id="listItem" resultMap="Bas ...

  6. Spring Boot 构建电商基础秒杀项目 (八) 商品创建

    SpringBoot构建电商基础秒杀项目 学习笔记 新建数据表 create table if not exists item ( id int not null auto_increment, ti ...

  7. Spring Boot 构建电商基础秒杀项目 (七) 自动校验

    SpringBoot构建电商基础秒杀项目 学习笔记 修改 UserModel 添加注解 public class UserModel { private Integer id; @NotBlank(m ...

  8. Spring Boot 构建电商基础秒杀项目 (六) 用户登陆

    SpringBoot构建电商基础秒杀项目 学习笔记 userDOMapper.xml 添加 <select id="selectByTelphone" resultMap=& ...

  9. Spring Boot 构建电商基础秒杀项目 (五) 用户注册

    SpringBoot构建电商基础秒杀项目 学习笔记 UserService 添加 void register(UserModel userModel) throws BusinessException ...

随机推荐

  1. 从零开始搭建django前后端分离项目 系列五(实战之excel流式导出)

    项目中有一处功能需求是:需要在历史数据查询页面进行查询字段的选择,然后由后台数据库动态生成对应的excel表格并下载到本地. 如果文件较小,解决办法是先将要传送的内容全生成在内存中,然后再一次性传入R ...

  2. Burp Suite学习之Intruder的4种攻击模式

    burp suit的intruder攻击共有四种模式,如图所示,下面分别讲讲这四种模式的使用方法和场景. 一 .Sniper模式 Sniper模式使用一组payload集合,它一次只使用一个paylo ...

  3. Nginx+IIS简单的部署

    随着互联网项目用户访问量不断上升,单点web服务器是无法满足大型高并发高负载的业务处理的,为了给web服务器做负载均衡方案,打算采用Nginx搭建负载均衡服务器,把用户请求分配到N个服务器来缓解服务器 ...

  4. 【Python入门只需20分钟】从安装到数据抓取、存储原来这么简单

    基于大众对Python的大肆吹捧和赞赏,作为一名Java从业人员,我本着批判与好奇的心态买了本python方面的书<毫无障碍学Python>.仅仅看了书前面一小部分的我......决定做一 ...

  5. Linux 命令(二)

    man help:线上查询及帮助命令 命令  --help:简单帮助 help  cd:查看一些Linux命令行的一些内置命令 文件和目操作命令(19个) ls  cd  cp  find  mkdi ...

  6. 十三、MUI的日期起始和结束日期设置

    MUI的日期选择器的使用 // 日期选择器 //生日选择器(不会超过今年) function fdPicker1(id) { var year=new Date().getFullYear(); va ...

  7. 【转载】SQL中inner join、outer join和cross join的区别

    对于SQL中inner join.outer join和cross join的区别很多人不知道,我也是别人问起,才查找资料看了下,跟自己之前的认识差不多, 如果你使用join连表,缺陷的情况下是inn ...

  8. UVA 10820 欧拉函数模板题

    这道题就是一道简单的欧拉函数模板题,需要注意的是,当(1,1)时只有一个,其他的都有一对.应该对欧拉函数做预处理,显然不会超时. #include<iostream> #include&l ...

  9. hdu 2063 给男女匹配 (匈牙利算法)

    来源:http://acm.hdu.edu.cn/showproblem.php?pid=2063 题意: 有k个组合a,b组合,代表a愿意与b坐过山车,共m个女生 n个男生,问有多少个满意的匹配 题 ...

  10. C++ string中的find()函数

    1.string中find()返回值是字母在母串中的位置(下标记录),如果没有找到,那么会返回一个特别的标记npos.(返回值可以看成是一个int型的数) #include<cstring> ...