From e94690a2b0c77fd7b816e1543c53910f8524c76a Mon Sep 17 00:00:00 2001 From: qiurunze Date: Mon, 10 Dec 2018 10:28:14 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E5=85=A8=E5=B1=80=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 +- .../miaosha/access/AccessInterceptor.java | 15 ++-- .../common/resultbean/ResultGeekQ.java | 2 - .../miaosha/controller/GoodsController.java | 1 - .../miaosha/controller/MiaoshaController.java | 21 +++-- .../miaosha/controller/OrderController.java | 2 - .../exception/GlobleExceptionHandler.java | 4 - .../com/geekq/miaosha/result/CodeMsg.java | 74 --------------- .../java/com/geekq/miaosha/result/Result.java | 90 ------------------- 9 files changed, 24 insertions(+), 191 deletions(-) delete mode 100644 src/main/java/com/geekq/miaosha/result/CodeMsg.java delete mode 100644 src/main/java/com/geekq/miaosha/result/Result.java diff --git a/README.md b/README.md index dac79f9..f7f6c7b 100644 --- a/README.md +++ b/README.md @@ -26,9 +26,9 @@ --数据库加唯一索引防止用户重复购买 --redis预减库存减少数据库访问 内存标记减少redis访问 请求先入队列缓冲,异步下单,增强用户体验 #### [1.全局异常处理拦截]() -> 1.定义全局的异常拦截器
-> 2.定义了全局异常类型
-> 3.只返回和业务有关的
+ 1.定义全局的异常拦截器
+ 2.定义了全局异常类型
+ 3.只返回和业务有关的
#### [解决分布式session]() --生成一个随机的uuid一类的写回到cookie中 diff --git a/src/main/java/com/geekq/miaosha/access/AccessInterceptor.java b/src/main/java/com/geekq/miaosha/access/AccessInterceptor.java index 4770b67..bb650d2 100644 --- a/src/main/java/com/geekq/miaosha/access/AccessInterceptor.java +++ b/src/main/java/com/geekq/miaosha/access/AccessInterceptor.java @@ -1,10 +1,10 @@ package com.geekq.miaosha.access; import com.alibaba.fastjson.JSON; +import com.geekq.miaosha.common.enums.ResultStatus; +import com.geekq.miaosha.common.resultbean.ResultGeekQ; import com.geekq.miaosha.domain.MiaoshaUser; import com.geekq.miaosha.redis.RedisService; -import com.geekq.miaosha.result.CodeMsg; -import com.geekq.miaosha.result.Result; import com.geekq.miaosha.service.MiaoShaUserService; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -17,6 +17,9 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.OutputStream; +import static com.geekq.miaosha.common.enums.ResultStatus.ACCESS_LIMIT_REACHED; +import static com.geekq.miaosha.common.enums.ResultStatus.SESSION_ERROR; + @Service public class AccessInterceptor extends HandlerInterceptorAdapter{ @@ -43,7 +46,7 @@ public class AccessInterceptor extends HandlerInterceptorAdapter{ String key = request.getRequestURI(); if(needLogin) { if(user == null) { - render(response, CodeMsg.SESSION_ERROR); + render(response, SESSION_ERROR); return false; } key += "_" + user.getId(); @@ -57,17 +60,17 @@ public class AccessInterceptor extends HandlerInterceptorAdapter{ }else if(count < maxCount) { redisService.incr(ak, key); }else { - render(response, CodeMsg.ACCESS_LIMIT_REACHED); + render(response, ACCESS_LIMIT_REACHED); return false; } } return true; } - private void render(HttpServletResponse response, CodeMsg cm)throws Exception { + private void render(HttpServletResponse response, ResultStatus cm)throws Exception { response.setContentType("application/json;charset=UTF-8"); OutputStream out = response.getOutputStream(); - String str = JSON.toJSONString(Result.error(cm)); + String str = JSON.toJSONString(ResultGeekQ.error(cm)); out.write(str.getBytes("UTF-8")); out.flush(); out.close(); diff --git a/src/main/java/com/geekq/miaosha/common/resultbean/ResultGeekQ.java b/src/main/java/com/geekq/miaosha/common/resultbean/ResultGeekQ.java index 681eda9..c84143b 100644 --- a/src/main/java/com/geekq/miaosha/common/resultbean/ResultGeekQ.java +++ b/src/main/java/com/geekq/miaosha/common/resultbean/ResultGeekQ.java @@ -1,8 +1,6 @@ package com.geekq.miaosha.common.resultbean; import com.geekq.miaosha.common.enums.ResultStatus; -import com.geekq.miaosha.result.CodeMsg; -import com.geekq.miaosha.result.Result; import java.io.Serializable; diff --git a/src/main/java/com/geekq/miaosha/controller/GoodsController.java b/src/main/java/com/geekq/miaosha/controller/GoodsController.java index f249cb3..4b5a2c6 100644 --- a/src/main/java/com/geekq/miaosha/controller/GoodsController.java +++ b/src/main/java/com/geekq/miaosha/controller/GoodsController.java @@ -4,7 +4,6 @@ import com.geekq.miaosha.common.resultbean.ResultGeekQ; import com.geekq.miaosha.domain.MiaoshaUser; import com.geekq.miaosha.redis.GoodsKey; import com.geekq.miaosha.redis.RedisService; -import com.geekq.miaosha.result.Result; import com.geekq.miaosha.service.GoodsService; import com.geekq.miaosha.service.MiaoShaUserService; import com.geekq.miaosha.vo.GoodsDetailVo; diff --git a/src/main/java/com/geekq/miaosha/controller/MiaoshaController.java b/src/main/java/com/geekq/miaosha/controller/MiaoshaController.java index b0f6f32..2a53479 100644 --- a/src/main/java/com/geekq/miaosha/controller/MiaoshaController.java +++ b/src/main/java/com/geekq/miaosha/controller/MiaoshaController.java @@ -1,7 +1,6 @@ package com.geekq.miaosha.controller; import com.geekq.miaosha.access.AccessLimit; -import com.geekq.miaosha.common.enums.ResultStatus; import com.geekq.miaosha.common.resultbean.ResultGeekQ; import com.geekq.miaosha.domain.MiaoshaOrder; import com.geekq.miaosha.domain.MiaoshaUser; @@ -9,13 +8,13 @@ import com.geekq.miaosha.rabbitmq.MQSender; import com.geekq.miaosha.rabbitmq.MiaoshaMessage; import com.geekq.miaosha.redis.GoodsKey; import com.geekq.miaosha.redis.RedisService; -import com.geekq.miaosha.result.CodeMsg; -import com.geekq.miaosha.result.Result; import com.geekq.miaosha.service.GoodsService; import com.geekq.miaosha.service.MiaoShaUserService; import com.geekq.miaosha.service.MiaoshaService; import com.geekq.miaosha.service.OrderService; import com.geekq.miaosha.vo.GoodsVo; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; @@ -36,6 +35,8 @@ import static com.geekq.miaosha.common.enums.ResultStatus.*; @RequestMapping("/miaosha") public class MiaoshaController implements InitializingBean { + private static Logger logger = LoggerFactory.getLogger(MiaoshaController.class); + @Autowired MiaoShaUserService userService; @@ -160,10 +161,12 @@ public class MiaoshaController implements InitializingBean { @RequestMapping(value = "/verifyCode", method = RequestMethod.GET) @ResponseBody - public Result getMiaoshaVerifyCod(HttpServletResponse response, MiaoshaUser user, + public ResultGeekQ getMiaoshaVerifyCod(HttpServletResponse response, MiaoshaUser user, @RequestParam("goodsId") long goodsId) { + ResultGeekQ result = ResultGeekQ.build(); if (user == null) { - return Result.error(CodeMsg.SESSION_ERROR); + result.withError(SESSION_ERROR.getCode(), SESSION_ERROR.getMessage()); + return result; } try { BufferedImage image = miaoshaService.createVerifyCode(user, goodsId); @@ -171,13 +174,13 @@ public class MiaoshaController implements InitializingBean { ImageIO.write(image, "JPEG", out); out.flush(); out.close(); - return null; + return result; } catch (Exception e) { - e.printStackTrace(); - return Result.error(CodeMsg.MIAOSHA_FAIL); + logger.error("生成验证码错误-----goodsId:{}", goodsId, e); + result.withError(MIAOSHA_FAIL.getCode(), MIAOSHA_FAIL.getMessage()); + return result; } } - /** * 系统初始化 * diff --git a/src/main/java/com/geekq/miaosha/controller/OrderController.java b/src/main/java/com/geekq/miaosha/controller/OrderController.java index e27c576..ef5802a 100644 --- a/src/main/java/com/geekq/miaosha/controller/OrderController.java +++ b/src/main/java/com/geekq/miaosha/controller/OrderController.java @@ -4,8 +4,6 @@ import com.geekq.miaosha.common.resultbean.ResultGeekQ; import com.geekq.miaosha.domain.MiaoshaUser; import com.geekq.miaosha.domain.OrderInfo; import com.geekq.miaosha.redis.RedisService; -import com.geekq.miaosha.result.CodeMsg; -import com.geekq.miaosha.result.Result; import com.geekq.miaosha.service.GoodsService; import com.geekq.miaosha.service.MiaoShaUserService; import com.geekq.miaosha.service.OrderService; diff --git a/src/main/java/com/geekq/miaosha/exception/GlobleExceptionHandler.java b/src/main/java/com/geekq/miaosha/exception/GlobleExceptionHandler.java index 3bd53db..db94fe1 100644 --- a/src/main/java/com/geekq/miaosha/exception/GlobleExceptionHandler.java +++ b/src/main/java/com/geekq/miaosha/exception/GlobleExceptionHandler.java @@ -1,9 +1,6 @@ package com.geekq.miaosha.exception; -import com.geekq.miaosha.common.enums.ResultStatus; import com.geekq.miaosha.common.resultbean.ResultGeekQ; -import com.geekq.miaosha.result.CodeMsg; -import com.geekq.miaosha.result.Result; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.validation.BindException; @@ -17,7 +14,6 @@ import java.util.List; import static com.geekq.miaosha.common.enums.ResultStatus.SESSION_ERROR; import static com.geekq.miaosha.common.enums.ResultStatus.SYSTEM_ERROR; -import static com.geekq.miaosha.result.CodeMsg.BIND_ERROR; /** * 拦截异常 diff --git a/src/main/java/com/geekq/miaosha/result/CodeMsg.java b/src/main/java/com/geekq/miaosha/result/CodeMsg.java deleted file mode 100644 index e890505..0000000 --- a/src/main/java/com/geekq/miaosha/result/CodeMsg.java +++ /dev/null @@ -1,74 +0,0 @@ -package com.geekq.miaosha.result; - -public class CodeMsg { - - private int code; - private String msg; - - //通用的错误码 - public static CodeMsg SUCCESS = new CodeMsg(0, "success"); - - public static CodeMsg SUCCESS_RESIGETER = new CodeMsg(200,"注册成功!"); - - public static CodeMsg RESIGETER_FAIL = new CodeMsg(300,"注册失败!"); - - public static CodeMsg SERVER_ERROR = new CodeMsg(500100, "服务端异常"); - public static CodeMsg BIND_ERROR = new CodeMsg(500101, "参数校验异常:%s"); - public static CodeMsg REQUEST_ILLEGAL = new CodeMsg(500102, "请求非法"); - public static CodeMsg ACCESS_LIMIT_REACHED= new CodeMsg(500104, "访问太频繁!"); - //登录模块 5002XX - public static CodeMsg SESSION_ERROR = new CodeMsg(500210, "Session不存在或者已经失效"); - public static CodeMsg PASSWORD_EMPTY = new CodeMsg(500211, "登录密码不能为空"); - public static CodeMsg MOBILE_EMPTY = new CodeMsg(500212, "手机号不能为空"); - public static CodeMsg MOBILE_ERROR = new CodeMsg(500213, "手机号格式错误"); - public static CodeMsg MOBILE_NOT_EXIST = new CodeMsg(500214, "手机号不存在"); - public static CodeMsg PASSWORD_ERROR = new CodeMsg(500215, "密码错误"); - - - //商品模块 5003XX - - - //订单模块 5004XX - public static CodeMsg ORDER_NOT_EXIST = new CodeMsg(500400, "订单不存在"); - - //秒杀模块 5005XX - public static CodeMsg MIAO_SHA_OVER = new CodeMsg(500500, "商品已经秒杀完毕"); - public static CodeMsg REPEATE_MIAOSHA = new CodeMsg(500501, "不能重复秒杀"); - public static CodeMsg MIAOSHA_FAIL = new CodeMsg(500502, "秒杀失败"); - - - private CodeMsg( ) { - } - - private CodeMsg( int code,String msg ) { - this.code = code; - this.msg = msg; - } - - public int getCode() { - return code; - } - public void setCode(int code) { - this.code = code; - } - public String getMsg() { - return msg; - } - public void setMsg(String msg) { - this.msg = msg; - } - - public CodeMsg fillArgs(Object... args) { - int code = this.code; - //拼接错误信息 - String message = String.format(this.msg, args); - return new CodeMsg(code, message); - } - - @Override - public String toString() { - return "CodeMsg [code=" + code + ", msg=" + msg + "]"; - } - - -} diff --git a/src/main/java/com/geekq/miaosha/result/Result.java b/src/main/java/com/geekq/miaosha/result/Result.java deleted file mode 100644 index 09b0f26..0000000 --- a/src/main/java/com/geekq/miaosha/result/Result.java +++ /dev/null @@ -1,90 +0,0 @@ -package com.geekq.miaosha.result; - -public class Result { - private int code; - private String msg; - private T data; - - - /** - * 成功时候的调用 - * - * @param data - * @param - * @return - */ - public static Result success(T data) { - return new Result(data); - } - - /** - * 注册时候调用 - * - * @param msg - * @param data - * @param - * @return - */ - public static Result success(CodeMsg msg) { - - return new Result(CodeMsg.SUCCESS_RESIGETER); - - } - - /** - * 失败的时候调用 - * - * @param cm - * @param - * @return - */ - public static Result error(CodeMsg cm) { - return new Result(cm); - } - -// public ResultGeekQ(CodeMsg msg, T data) { -// this.code = msg.getCode(); -// this.msg = msg.getMsg(); -// this.data = data; -// } - - private Result(CodeMsg cm) { - if (cm == null) { - return; - } - this.code = cm.getCode(); - this.msg = cm.getMsg(); - } - - - private Result(T data) { - this.code = 0; - this.msg = "success"; - this.data = data; - } - - public int getCode() { - return code; - } - - public void setCode(int code) { - this.code = code; - } - - public String getMsg() { - return msg; - } - - public void setMsg(String msg) { - this.msg = msg; - } - - public T getData() { - return data; - } - - public void setData(T data) { - this.data = data; - } -} -