Skip to content

Commit

Permalink
提交全局异常修改
Browse files Browse the repository at this point in the history
  • Loading branch information
qiurunze committed Dec 10, 2018
1 parent 3358215 commit e94690a
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 191 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
--数据库加唯一索引防止用户重复购买
--redis预减库存减少数据库访问 内存标记减少redis访问 请求先入队列缓冲,异步下单,增强用户体验
#### [1.全局异常处理拦截]()
> 1.定义全局的异常拦截器<br>
> 2.定义了全局异常类型<br>
> 3.只返回和业务有关的<br>
1.定义全局的异常拦截器<br>
2.定义了全局异常类型<br>
3.只返回和业务有关的<br>

#### [解决分布式session]()
--生成一个随机的uuid一类的写回到cookie中
Expand Down
15 changes: 9 additions & 6 deletions src/main/java/com/geekq/miaosha/access/AccessInterceptor.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -17,6 +17,9 @@
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{

Expand All @@ -43,7 +46,7 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons
String key = request.getRequestURI();
if(needLogin) {
if(user == null) {
render(response, CodeMsg.SESSION_ERROR);
render(response, SESSION_ERROR);
return false;
}
key += "_" + user.getId();
Expand All @@ -57,17 +60,17 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons
}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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
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;
Expand Down
21 changes: 12 additions & 9 deletions src/main/java/com/geekq/miaosha/controller/MiaoshaController.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
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;
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;
Expand All @@ -36,6 +35,8 @@
@RequestMapping("/miaosha")
public class MiaoshaController implements InitializingBean {

private static Logger logger = LoggerFactory.getLogger(MiaoshaController.class);

@Autowired
MiaoShaUserService userService;

Expand Down Expand Up @@ -160,24 +161,26 @@ public ResultGeekQ<String> getMiaoshaPath(HttpServletRequest request, MiaoshaUse

@RequestMapping(value = "/verifyCode", method = RequestMethod.GET)
@ResponseBody
public Result<String> getMiaoshaVerifyCod(HttpServletResponse response, MiaoshaUser user,
public ResultGeekQ<String> getMiaoshaVerifyCod(HttpServletResponse response, MiaoshaUser user,
@RequestParam("goodsId") long goodsId) {
ResultGeekQ<String> 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);
OutputStream out = response.getOutputStream();
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;
}
}

/**
* 系统初始化
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -17,7 +14,6 @@

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;

/**
* 拦截异常
Expand Down
74 changes: 0 additions & 74 deletions src/main/java/com/geekq/miaosha/result/CodeMsg.java

This file was deleted.

90 changes: 0 additions & 90 deletions src/main/java/com/geekq/miaosha/result/Result.java

This file was deleted.

0 comments on commit e94690a

Please sign in to comment.