mirror of
https://github.com/qiurunze123/miaosha.git
synced 2023-11-19 22:41:03 +08:00
提交全局异常修改
This commit is contained in:
parent
3358215bc5
commit
e94690a2b0
|
@ -26,9 +26,9 @@
|
|||
--数据库加唯一索引防止用户重复购买
|
||||
--redis预减库存减少数据库访问 内存标记减少redis访问 请求先入队列缓冲,异步下单,增强用户体验
|
||||
#### [1.全局异常处理拦截]()
|
||||
> 1.定义全局的异常拦截器<br>
|
||||
> 2.定义了全局异常类型<br>
|
||||
> 3.只返回和业务有关的<br>
|
||||
1.定义全局的异常拦截器<br>
|
||||
2.定义了全局异常类型<br>
|
||||
3.只返回和业务有关的<br>
|
||||
|
||||
#### [解决分布式session]()
|
||||
--生成一个随机的uuid一类的写回到cookie中
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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<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);
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 系统初始化
|
||||
*
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
||||
/**
|
||||
* 拦截异常
|
||||
|
|
|
@ -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 + "]";
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -1,90 +0,0 @@
|
|||
package com.geekq.miaosha.result;
|
||||
|
||||
public class Result<T> {
|
||||
private int code;
|
||||
private String msg;
|
||||
private T data;
|
||||
|
||||
|
||||
/**
|
||||
* 成功时候的调用
|
||||
*
|
||||
* @param data
|
||||
* @param <T>
|
||||
* @return
|
||||
*/
|
||||
public static <T> Result<T> success(T data) {
|
||||
return new Result<T>(data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册时候调用
|
||||
*
|
||||
* @param msg
|
||||
* @param data
|
||||
* @param <T>
|
||||
* @return
|
||||
*/
|
||||
public static <T> Result<T> success(CodeMsg msg) {
|
||||
|
||||
return new Result<T>(CodeMsg.SUCCESS_RESIGETER);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 失败的时候调用
|
||||
*
|
||||
* @param cm
|
||||
* @param <T>
|
||||
* @return
|
||||
*/
|
||||
public static <T> Result<T> error(CodeMsg cm) {
|
||||
return new Result<T>(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;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user