小程序限流返回统一状态码
This commit is contained in:
@ -17,6 +17,7 @@ import co.yixiang.api.UnAuthenticatedException;
|
||||
import co.yixiang.api.YshopException;
|
||||
import co.yixiang.common.bean.RequestDetail;
|
||||
import co.yixiang.common.util.RequestDetailThreadLocal;
|
||||
import co.yixiang.exception.BadLimitRequestException;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpStatus;
|
||||
@ -72,6 +73,18 @@ public class GlobalExceptionHandler {
|
||||
|
||||
|
||||
|
||||
@ExceptionHandler(value = BadLimitRequestException.class)
|
||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||
public ApiResult<Boolean> badLimitRequestException(BadLimitRequestException exception) {
|
||||
printRequestDetail();
|
||||
printApiCodeException(ApiCode.SYSTEM_EXCEPTION, exception);
|
||||
return new ApiResult<Boolean>()
|
||||
.setStatus(ApiCode.BAD_LIMIT_EXCEPTION.getCode())
|
||||
.setMsg(exception.getMessage());
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 自定义业务/数据异常处理
|
||||
*
|
||||
|
@ -0,0 +1,33 @@
|
||||
package co.yixiang.modules.test;
|
||||
|
||||
import co.yixiang.annotation.AnonymousAccess;
|
||||
import co.yixiang.annotation.Limit;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
/**
|
||||
* @author /
|
||||
* 接口限流测试类
|
||||
*/
|
||||
@RestController
|
||||
@Api(tags = "系统:限流测试管理")
|
||||
public class LimitController {
|
||||
|
||||
private static final AtomicInteger ATOMIC_INTEGER = new AtomicInteger();
|
||||
|
||||
/**
|
||||
* 测试限流注解,下面配置说明该接口 60秒内最多只能访问 10次,保存到redis的键名为 limit_test,
|
||||
*/
|
||||
@GetMapping("test")
|
||||
@AnonymousAccess
|
||||
@ApiOperation("测试")
|
||||
@Limit(key = "test", period = 60, count = 10, name = "testLimit", prefix = "limit")
|
||||
public int testLimit() {
|
||||
return ATOMIC_INTEGER.incrementAndGet();
|
||||
}
|
||||
}
|
@ -91,6 +91,10 @@ public enum ApiCode {
|
||||
|
||||
HTTP_REQUEST_METHOD_NOT_SUPPORTED_EXCEPTION(5108, "METHOD NOT SUPPORTED"),
|
||||
|
||||
/**
|
||||
* 访问次数受限制
|
||||
**/
|
||||
BAD_LIMIT_EXCEPTION(5109, "访问次数受限制"),
|
||||
;
|
||||
|
||||
private final int code;
|
||||
|
@ -6,6 +6,7 @@
|
||||
package co.yixiang.aspect;
|
||||
|
||||
import co.yixiang.annotation.Limit;
|
||||
import co.yixiang.exception.BadLimitRequestException;
|
||||
import co.yixiang.exception.BadRequestException;
|
||||
import co.yixiang.utils.RequestHolder;
|
||||
import co.yixiang.utils.StringUtils;
|
||||
@ -68,7 +69,7 @@ public class LimitAspect {
|
||||
logger.info("第{}次访问key为 {},描述为 [{}] 的接口", count, keys, limit.name());
|
||||
return joinPoint.proceed();
|
||||
} else {
|
||||
throw new BadRequestException("访问次数受限制");
|
||||
throw new BadLimitRequestException("访问次数受限制");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,32 @@
|
||||
/**
|
||||
* Copyright (C) 2018-2020
|
||||
* All rights reserved, Designed By www.yixiang.co
|
||||
|
||||
*/
|
||||
package co.yixiang.exception;
|
||||
|
||||
import co.yixiang.api.ApiCode;
|
||||
import lombok.Getter;
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
||||
import static org.springframework.http.HttpStatus.BAD_REQUEST;
|
||||
|
||||
/**
|
||||
* @author Zheng Jie
|
||||
* @date 2018-11-23
|
||||
* 统一异常处理
|
||||
*/
|
||||
@Getter
|
||||
public class BadLimitRequestException extends RuntimeException{
|
||||
|
||||
private Integer status = ApiCode.BAD_LIMIT_EXCEPTION.getCode();
|
||||
|
||||
public BadLimitRequestException(String msg){
|
||||
super(msg);
|
||||
}
|
||||
|
||||
public BadLimitRequestException(HttpStatus status, String msg){
|
||||
super(msg);
|
||||
this.status = status.value();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user