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. git中Please enter a commit message to explain why this merge is necessary.

    Please enter a commit message to explain why this merge is necessary. 请输入提交消息来解释为什么这种合并是必要的 git 在pul ...

  2. 史上最全面的Neo4j使用指南

    Neo4j图形数据库教程 Neo4j图形数据库教程 第一章:介绍 Neo4j是什么 Neo4j的特点 Neo4j的优点 第二章:安装 1.环境 2.下载 3.开启远程访问 4.测试 第三章:CQL 1 ...

  3. 面试 9:Java 玩转冒泡排序

    面试 9:用 Java 实现冒泡排序 南尘的朋友们,新的一周好,原本打算继续讲链表考点算法的,这里姑且是卡一段.虽然在我们 Android 开发中,很少涉及到排序算法,因为基本官方都帮我们封装好了,但 ...

  4. 五子棋(无AI winform gdi+)

    之前无意间在博客园看到一篇用深度学习玩马里奥的文章,于是就想做这个小东西来测试人工智能算法(准备用PYTHON的库,对神经网络的梦已经做了好多年了,但是太难了,一直懒得动它),本来是想用WPF做UI, ...

  5. flask实现子域名

    什么是子域名? 子域名,类似于xxx.douban.com的形式,如book.douban.com,music.douban.com,movie.douban.com等 用flask怎么实现子域名? ...

  6. 数组去重--ES5和ES6

    思路:把去重后的数组放到一个空数组中 ES5实现: function uni(arr) { var result = []; for (var i=0;i<arr.length;i++) { i ...

  7. 出题人的RP值(牛客练习赛38--A题)(排序)

    链接:https://ac.nowcoder.com/acm/contest/358/A来源:牛客网 题目描述 众所周知,每个人都有自己的rp值(是个非负实数),膜别人可以从别人身上吸取rp值. 然而 ...

  8. NSAssert和NSParameterAssert

    2016.05.05 18:34* 字数 861 阅读 5127评论 0喜欢 17 https://www.jianshu.com/p/3072e174554f NSAssert和NSParamete ...

  9. Memcache之安装篇

    本篇文章会介绍memcache在Windows和Linux下的具体安装过程,详细的记录其中的流程内容,帮助小伙伴们快速的搭建起memcache服务,废话少说,直接上!!! Windows: Memca ...

  10. java的static与C#的static的异同

    static static同样可以用在类.方法.变量上面,但是在java和C#中所表示的意思完全不同,我个人的总结是C#中的静态和非静态是有一个明显的分界的,静态的是属于类级别的,而非静态的是属于实例 ...