From 0724f1019638ff1e348d7afbb12c9ce50dfa80a0 Mon Sep 17 00:00:00 2001 From: koltZhang Date: Sat, 31 Aug 2024 15:15:50 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=84=E8=8C=83=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SentinelExceptionHandlerAutoConfiguration.java | 7 +++++-- .../core/handler/SentinelExceptionHandler.java | 14 ++++++++------ 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/yudao-framework/yudao-spring-boot-starter-sentinel/src/main/java/cn/iocoder/yudao/framework/sentinel/config/SentinelExceptionHandlerAutoConfiguration.java b/yudao-framework/yudao-spring-boot-starter-sentinel/src/main/java/cn/iocoder/yudao/framework/sentinel/config/SentinelExceptionHandlerAutoConfiguration.java index 7854de433..dddcb8093 100644 --- a/yudao-framework/yudao-spring-boot-starter-sentinel/src/main/java/cn/iocoder/yudao/framework/sentinel/config/SentinelExceptionHandlerAutoConfiguration.java +++ b/yudao-framework/yudao-spring-boot-starter-sentinel/src/main/java/cn/iocoder/yudao/framework/sentinel/config/SentinelExceptionHandlerAutoConfiguration.java @@ -6,13 +6,16 @@ import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; +/** + * 当Spring容器中不存在MyService类型的bean时,才创建这个bean + */ @AutoConfiguration @Slf4j public class SentinelExceptionHandlerAutoConfiguration { @Bean - @ConditionalOnMissingBean // 当Spring容器中不存在MyService类型的bean时,才创建这个bean + @ConditionalOnMissingBean public SentinelExceptionHandler sentinelExceptionHandler() { return new SentinelExceptionHandler(); } -} \ No newline at end of file +} \ No newline at end of file diff --git a/yudao-framework/yudao-spring-boot-starter-sentinel/src/main/java/cn/iocoder/yudao/framework/sentinel/core/handler/SentinelExceptionHandler.java b/yudao-framework/yudao-spring-boot-starter-sentinel/src/main/java/cn/iocoder/yudao/framework/sentinel/core/handler/SentinelExceptionHandler.java index f5501f7b7..a7ef3b06e 100644 --- a/yudao-framework/yudao-spring-boot-starter-sentinel/src/main/java/cn/iocoder/yudao/framework/sentinel/core/handler/SentinelExceptionHandler.java +++ b/yudao-framework/yudao-spring-boot-starter-sentinel/src/main/java/cn/iocoder/yudao/framework/sentinel/core/handler/SentinelExceptionHandler.java @@ -1,6 +1,7 @@ package cn.iocoder.yudao.framework.sentinel.core.handler; import cn.iocoder.yudao.framework.common.pojo.CommonResult; +import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils; import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler; import com.alibaba.csp.sentinel.slots.block.BlockException; import com.alibaba.csp.sentinel.slots.block.authority.AuthorityException; @@ -8,16 +9,19 @@ import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException; import com.alibaba.csp.sentinel.slots.block.flow.FlowException; import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException; import com.alibaba.csp.sentinel.slots.system.SystemBlockException; -import com.alibaba.fastjson.JSON; import org.springframework.http.HttpStatus; -import org.springframework.http.MediaType; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +/** + * Sentinel自定义阻断异常处理 + */ public class SentinelExceptionHandler implements BlockExceptionHandler { + @Override - public void handle(HttpServletRequest request, HttpServletResponse response, BlockException e) throws Exception { + public void handle(HttpServletRequest request, HttpServletResponse response, BlockException e) { + String msg = "未知异常"; int status = HttpStatus.TOO_MANY_REQUESTS.value(); if (e instanceof FlowException) { @@ -33,8 +37,6 @@ public class SentinelExceptionHandler implements BlockExceptionHandler { msg = "系统规则限流或降级"; } - response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE); - response.setStatus(status); - response.getOutputStream().println(JSON.toJSONString(CommonResult.error(status,msg))); + ServletUtils.writeJSON(response, CommonResult.error(status, msg)); } } \ No newline at end of file