+ * [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()); - } - -}