优化 [Spring Boot 3.2.0] 404 Not Found behavior #38733

This commit is contained in:
lbw 2024-01-07 13:51:33 +08:00
parent 9617a28cc3
commit ef2b82d605
2 changed files with 139 additions and 112 deletions

View File

@ -63,5 +63,10 @@
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
</dependency>
<!-- 异常枚举 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -31,6 +31,7 @@ import org.springframework.web.bind.MethodArgumentNotValidException;
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.servlet.resource.NoResourceFoundException;
import java.util.List;
@ -50,6 +51,7 @@ public class GlobalBizExceptionHandler {
/**
* 全局异常.
*
* @param e the e
* @return R
*/
@ -65,13 +67,14 @@ public class GlobalBizExceptionHandler {
/**
* 处理业务校验过程中碰到的非法参数异常 该异常基本由{@link org.springframework.util.Assert}抛出
*
* @param exception 参数校验异常
* @return API返回结果对象包装后的错误输出结果
* @see Assert#hasLength(String, String)
* @see Assert#hasText(String, String)
* @see Assert#isTrue(boolean, String)
* @see Assert#isNull(Object, String)
* @see Assert#notNull(Object, String)
* @param exception 参数校验异常
* @return API返回结果对象包装后的错误输出结果
*/
@ExceptionHandler(IllegalArgumentException.class)
@ResponseStatus(HttpStatus.OK)
@ -82,6 +85,7 @@ public class GlobalBizExceptionHandler {
/**
* AccessDeniedException
*
* @param e the e
* @return R
*/
@ -96,6 +100,7 @@ public class GlobalBizExceptionHandler {
/**
* validation Exception
*
* @param exception
* @return R
*/
@ -109,6 +114,7 @@ public class GlobalBizExceptionHandler {
/**
* validation Exception (以form-data形式传参)
*
* @param exception
* @return R
*/
@ -120,4 +126,20 @@ public class GlobalBizExceptionHandler {
return R.failed(fieldErrors.get(0).getDefaultMessage());
}
/**
* 保持和低版本请求路径不存在的行为一致
* <p>
* <a href="https://github.com/spring-projects/spring-boot/issues/38733">[Spring Boot 3.2.0] 404 Not Found behavior #38733</a>
*
* @param exception
* @return R
*/
@ExceptionHandler({NoResourceFoundException.class})
@ResponseStatus(HttpStatus.NOT_FOUND)
public R bindExceptionHandler(NoResourceFoundException exception) {
log.debug("请求路径 404 {}", exception.getMessage());
return R.failed(exception.getMessage());
}
}