小程序限流返回统一状态码
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();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user