开发者学堂课程【精通Spring Cloud Alibaba:Gateway整合sentineI限流返回自定义提示】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/634/detail/10148
Gateway 整合 sentineI 限流返回自定义提示
如何修改限流错误提示
1、自定义一个 handler
2、输入
public class JsonSentinelGatewayBlockExceptionHandler implements WebExceptionHandler
{
public JsonSentinelGatewayBlockExceptionHandler(List< ViewResolver> viewResolvers,
ServerCodecConfigurer serverCodecConfigurer) {
}
@Override
public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {
ServerHttpResponse serverHttpResponse = exchange. getResponse( );
serverHttpResponse, getHeaders( ). add("Content-Type",
"application/json: charset=UTF-8");
byte[ ] datas = "{\"code\":403,\"msg\":\"API接口被限流
\"}".getBytes(StandardCharsets.UTF_8);
DataBuffer buffer = serverHttpResponse.bufferFactory( ) . wrap(datas);
return serverHttpResponse,writeWith(Mono. just(buffer));
}
}
3、结果显示:
{“code”:403,“msg”:“API接口被限流”}