diff --git a/pigx-common/pigx-common-gateway/pom.xml b/pigx-common/pigx-common-gateway/pom.xml index f5da10422..15e6784a0 100644 --- a/pigx-common/pigx-common-gateway/pom.xml +++ b/pigx-common/pigx-common-gateway/pom.xml @@ -59,5 +59,10 @@ org.springframework.cloud spring-cloud-starter-loadbalancer + + org.springframework + spring-webflux + true + diff --git a/pigx-common/pigx-common-gateway/src/main/java/com/pig4cloud/pigx/common/gateway/configuration/GlobalExceptionConfiguration.java b/pigx-common/pigx-common-gateway/src/main/java/com/pig4cloud/pigx/common/gateway/configuration/GlobalExceptionConfiguration.java index 7537623d6..773d23c43 100644 --- a/pigx-common/pigx-common-gateway/src/main/java/com/pig4cloud/pigx/common/gateway/configuration/GlobalExceptionConfiguration.java +++ b/pigx-common/pigx-common-gateway/src/main/java/com/pig4cloud/pigx/common/gateway/configuration/GlobalExceptionConfiguration.java @@ -9,11 +9,16 @@ import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler; import org.springframework.context.annotation.Configuration; import org.springframework.core.annotation.Order; import org.springframework.core.io.buffer.DataBufferFactory; +import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.server.reactive.ServerHttpResponse; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestControllerAdvice; import org.springframework.web.server.ResponseStatusException; import org.springframework.web.server.ServerWebExchange; import reactor.core.publisher.Mono; +import org.springframework.web.reactive.resource.NoResourceFoundException; /** * @author lengleng @@ -27,34 +32,54 @@ import reactor.core.publisher.Mono; @RequiredArgsConstructor public class GlobalExceptionConfiguration implements ErrorWebExceptionHandler { - private final ObjectMapper objectMapper; + private final ObjectMapper objectMapper; - @Override - public Mono handle(ServerWebExchange exchange, Throwable ex) { - ServerHttpResponse response = exchange.getResponse(); + @Override + public Mono handle(ServerWebExchange exchange, Throwable ex) { + ServerHttpResponse response = exchange.getResponse(); - if (response.isCommitted()) { - return Mono.error(ex); - } + if (response.isCommitted()) { + return Mono.error(ex); + } - // header set - response.getHeaders().setContentType(MediaType.APPLICATION_JSON); - if (ex instanceof ResponseStatusException) { - response.setStatusCode(((ResponseStatusException) ex).getStatusCode()); - } + // header set + response.getHeaders().setContentType(MediaType.APPLICATION_JSON); + if (ex instanceof ResponseStatusException) { + response.setStatusCode(((ResponseStatusException) ex).getStatusCode()); + } - return response.writeWith(Mono.fromSupplier(() -> { - DataBufferFactory bufferFactory = response.bufferFactory(); - try { - log.error("Error request :{} Error Spring Cloud Gateway : {}", exchange.getRequest().getPath(), - ex.getMessage()); - return bufferFactory.wrap(objectMapper.writeValueAsBytes(R.failed(ex.getMessage()))); - } - catch (JsonProcessingException e) { - log.warn("Error writing response", ex); - return bufferFactory.wrap(new byte[0]); - } - })); - } + return response.writeWith(Mono.fromSupplier(() -> { + DataBufferFactory bufferFactory = response.bufferFactory(); + try { + log.error("Error request :{} Error Spring Cloud Gateway : {}", exchange.getRequest().getPath(), + ex.getMessage()); + return bufferFactory.wrap(objectMapper.writeValueAsBytes(R.failed(ex.getMessage()))); + } catch (JsonProcessingException e) { + log.warn("Error writing response", ex); + return bufferFactory.wrap(new byte[0]); + } + })); + } + +} + + +/** + * 保持和低版本请求路径不存在的行为一致 + *

+ * [Spring Boot + * 3.2.0] 404 Not Found behavior #38733 + */ +@Slf4j +@RestControllerAdvice +class ReactiveNoResourceFoundHandler { + + + @ExceptionHandler({NoResourceFoundException.class}) + @ResponseStatus(HttpStatus.NOT_FOUND) + public R noResourceFoundException(NoResourceFoundException exception) { + log.debug("请求路径 404 {}", exception.getMessage()); + return R.failed(exception.getMessage()); + } } diff --git a/pigx-common/pigx-common-sentinel/src/main/java/com/pig4cloud/pigx/common/sentinel/PigxSentinelAutoConfiguration.java b/pigx-common/pigx-common-sentinel/src/main/java/com/pig4cloud/pigx/common/sentinel/PigxSentinelAutoConfiguration.java index 69218e0a1..6bf549674 100755 --- a/pigx-common/pigx-common-sentinel/src/main/java/com/pig4cloud/pigx/common/sentinel/PigxSentinelAutoConfiguration.java +++ b/pigx-common/pigx-common-sentinel/src/main/java/com/pig4cloud/pigx/common/sentinel/PigxSentinelAutoConfiguration.java @@ -23,14 +23,12 @@ import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.RequestOriginPars import com.pig4cloud.pigx.common.sentinel.feign.PigxSentinelFeign; import com.pig4cloud.pigx.common.sentinel.handle.GlobalBizExceptionHandler; import com.pig4cloud.pigx.common.sentinel.handle.PigxUrlBlockHandler; -import com.pig4cloud.pigx.common.sentinel.handle.ReactiveNoResourceFoundHandler; -import com.pig4cloud.pigx.common.sentinel.handle.WebNoResourceFoundHandler; import com.pig4cloud.pigx.common.sentinel.parser.PigxHeaderRequestOriginParser; import feign.Feign; import org.springframework.boot.autoconfigure.AutoConfigureBefore; -import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Scope; @@ -77,28 +75,11 @@ public class PigxSentinelAutoConfiguration { * 创建一个全局业务异常处理器实例 * 用于处理全局的业务异常 */ + @Bean + @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) public GlobalBizExceptionHandler globalBizExceptionHandler() { return new GlobalBizExceptionHandler(); } - /** - * 创建一个响应式资源未找到异常处理器实例 - * 如果存在org.springframework.web.reactive.resource.NoResourceFoundException类,则创建一个ReactiveNoResourceFoundHandler实例 - */ - @Bean - @ConditionalOnClass(name = {"org.springframework.web.reactive.resource.NoResourceFoundException"}) - public ReactiveNoResourceFoundHandler reactiveNoResourceFoundHandler() { - return new ReactiveNoResourceFoundHandler(); - } - - /** - * 创建一个Web资源未找到异常处理器实例 - * 如果存在org.springframework.web.servlet.resource.NoResourceFoundException类,则创建一个WebNoResourceFoundHandler实例 - */ - @Bean - @ConditionalOnClass(name = {"org.springframework.web.servlet.resource.NoResourceFoundException"}) - public WebNoResourceFoundHandler webNoResourceFoundHandler() { - return new WebNoResourceFoundHandler(); - } } diff --git a/pigx-common/pigx-common-sentinel/src/main/java/com/pig4cloud/pigx/common/sentinel/handle/GlobalBizExceptionHandler.java b/pigx-common/pigx-common-sentinel/src/main/java/com/pig4cloud/pigx/common/sentinel/handle/GlobalBizExceptionHandler.java index c2d053987..9c2485eeb 100644 --- a/pigx-common/pigx-common-sentinel/src/main/java/com/pig4cloud/pigx/common/sentinel/handle/GlobalBizExceptionHandler.java +++ b/pigx-common/pigx-common-sentinel/src/main/java/com/pig4cloud/pigx/common/sentinel/handle/GlobalBizExceptionHandler.java @@ -28,6 +28,7 @@ import org.springframework.security.access.AccessDeniedException; import org.springframework.validation.BindException; import org.springframework.validation.FieldError; import org.springframework.web.bind.annotation.*; +import org.springframework.web.servlet.resource.NoResourceFoundException; import java.util.List; @@ -36,7 +37,6 @@ import java.util.List; * @date 2020-06-29 */ @Slf4j -@RestController @RestControllerAdvice public class GlobalBizExceptionHandler { @@ -109,4 +109,20 @@ public class GlobalBizExceptionHandler { return R.failed(HttpStatus.NOT_FOUND.getReasonPhrase()); } + /** + * 保持和低版本请求路径不存在的行为一致 + *

+ * [Spring Boot + * 3.2.0] 404 Not Found behavior #38733 + * + * @param exception + * @return R + */ + @ExceptionHandler({NoResourceFoundException.class}) + @ResponseStatus(HttpStatus.NOT_FOUND) + public R noResourceFoundException(NoResourceFoundException exception) { + log.debug("请求路径 404 {}", exception.getMessage()); + return R.failed(exception.getMessage()); + } + } diff --git a/pigx-common/pigx-common-sentinel/src/main/java/com/pig4cloud/pigx/common/sentinel/handle/ReactiveNoResourceFoundHandler.java b/pigx-common/pigx-common-sentinel/src/main/java/com/pig4cloud/pigx/common/sentinel/handle/ReactiveNoResourceFoundHandler.java deleted file mode 100644 index 0fc19f13a..000000000 --- a/pigx-common/pigx-common-sentinel/src/main/java/com/pig4cloud/pigx/common/sentinel/handle/ReactiveNoResourceFoundHandler.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (c) 2018-2025, lengleng All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * Neither the name of the pig4cloud.com developer nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * Author: lengleng (wangiegie@gmail.com) - */ - -package com.pig4cloud.pigx.common.sentinel.handle; - -import com.pig4cloud.pigx.common.core.util.R; -import lombok.extern.slf4j.Slf4j; -import org.springframework.http.HttpStatus; -import org.springframework.web.bind.annotation.ExceptionHandler; -import org.springframework.web.bind.annotation.ResponseStatus; -import org.springframework.web.bind.annotation.RestController; -import org.springframework.web.bind.annotation.RestControllerAdvice; -import org.springframework.web.reactive.resource.NoResourceFoundException; - -/** - * @author lengleng - * @date 2020-06-29 - */ -@Slf4j -@RestController -@RestControllerAdvice -public class ReactiveNoResourceFoundHandler { - - /** - * 保持和低版本请求路径不存在的行为一致 - *

- * [Spring Boot - * 3.2.0] 404 Not Found behavior #38733 - * - * @param exception - * @return R - */ - @ExceptionHandler({NoResourceFoundException.class}) - @ResponseStatus(HttpStatus.NOT_FOUND) - public R noResourceFoundException(NoResourceFoundException exception) { - log.debug("请求路径 404 {}", exception.getMessage()); - return R.failed(exception.getMessage()); - } - -} diff --git a/pigx-common/pigx-common-sentinel/src/main/java/com/pig4cloud/pigx/common/sentinel/handle/WebNoResourceFoundHandler.java b/pigx-common/pigx-common-sentinel/src/main/java/com/pig4cloud/pigx/common/sentinel/handle/WebNoResourceFoundHandler.java deleted file mode 100644 index 8899e4b25..000000000 --- a/pigx-common/pigx-common-sentinel/src/main/java/com/pig4cloud/pigx/common/sentinel/handle/WebNoResourceFoundHandler.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (c) 2018-2025, lengleng All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * Neither the name of the pig4cloud.com developer nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * Author: lengleng (wangiegie@gmail.com) - */ - -package com.pig4cloud.pigx.common.sentinel.handle; - -import com.pig4cloud.pigx.common.core.util.R; -import lombok.extern.slf4j.Slf4j; -import org.springframework.http.HttpStatus; -import org.springframework.web.bind.annotation.ExceptionHandler; -import org.springframework.web.bind.annotation.ResponseStatus; -import org.springframework.web.bind.annotation.RestController; -import org.springframework.web.bind.annotation.RestControllerAdvice; -import org.springframework.web.servlet.resource.NoResourceFoundException; - -/** - * @author lengleng - * @date 2020-06-29 - */ -@Slf4j -@RestController -@RestControllerAdvice -public class WebNoResourceFoundHandler { - - /** - * 保持和低版本请求路径不存在的行为一致 - *

- * [Spring Boot - * 3.2.0] 404 Not Found behavior #38733 - * - * @param exception - * @return R - */ - @ExceptionHandler({NoResourceFoundException.class}) - @ResponseStatus(HttpStatus.NOT_FOUND) - public R noResourceFoundException(NoResourceFoundException exception) { - log.debug("请求路径 404 {}", exception.getMessage()); - return R.failed(exception.getMessage()); - } - -}