gpt4 book ai didi

java - 如何自定义 DefaultHandlerExceptionResolver 逻辑?

转载 作者:行者123 更新时间:2023-12-01 14:17:33 25 4
gpt4 key购买 nike

我要定制DefaultHandlerExceptionResolver在我的 Spring Boot 应用程序中,但在发生异常时从未达到自定义实现。

build.gradle

buildscript {
ext {
springBootVersion = '2.1.1.RELEASE'
}
repositories {
mavenCentral()
}
}

plugins {
id 'java'
id 'org.springframework.boot' version '2.1.1.RELEASE'
id 'io.spring.dependency-management' version '1.0.6.RELEASE'
id 'io.franzbecker.gradle-lombok' version '1.14'
}

lombok {
version = '1.18.4'
sha256 = ""
}

group = 'ua.com.javaman'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
mavenCentral()
}

dependencies {
implementation('org.springframework.boot:spring-boot-starter-web')
}

应用程序.java

@SpringBootApplication
@RestController
@Configuration
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

@ResponseBody
@PostMapping("/mouse")
public Mouse postMouse(@Valid Mouse mouse) {
// mouse creation logic
return mouse;
}

@Bean
public HandlerExceptionResolver customHandlerExceptionResolver() {
return new CustomExceptionHandlerResolver();
}
}

Mouse.java

@Value
public class Mouse {
private final Long id;
@NotEmpty
@Min(2)
private final String name;
}

CustomExceptionHandlerResolver.java

public class CustomExceptionHandlerResolver extends DefaultHandlerExceptionResolver {
@Override
protected ModelAndView handleBindException(
BindException ex, HttpServletRequest request, HttpServletResponse response, @Nullable Object handler
) throws IOException {
System.out.println("In CustomExceptionHandlerResolver");
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return new ModelAndView();
}
}

封装结构
.
├── build.gradle
├── gradle
│   └── wrapper
│   ├── gradle-wrapper.jar
│   └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── LICENSE
├── README.md
├── settings.gradle
└── src
└── main
└── java
└── ua
└── com
└── javaman
└── exception_handling
├── Application.java
├── CustomExceptionHandlerResolver.java
└── Mouse.java

当我为 Mouse 调用具有错误值的应用程序时实体,例如,
POST localhost:8080/mouse?name=1

handleBindException() DefaultHandlerExceptionResolver 中的方法被调用但不在我的 CustomExceptionHandlerResolver 中.

我该如何处理 BindException和我的 CustomExceptionHandlerResolver ?

最佳答案

SpringBoot 的自动配置将始终创建 a bunch of exception handlers默认情况下,其中之一是 DefaultHandlerExceptionResolver在 0 阶(较低的值具有较高的优先级)。默认情况下,您的处理程序的优先级低于这些 default ,因此不会调用它,因为默认情况下已经处理了异常。

您可以使用 WebMvcConfigurer 实现您的配置类并覆盖 extendHandlerExceptionResolvers()修改默认设置。这个想法是找出DefaultHandlerExceptionResolver实例并将其替换为您的实例。

但更好的主意是定义您的 CustomExceptionHandlerResolver@ControllerAdvice ,确保您不会弄乱默认设置,同时仍然添加您想要的自定义行为(即使用您的逻辑来处理 BindException ):

@Component
@ControllerAdvice
public class CustomExceptionHandlerResolver {

@ExceptionHandler(value= BindException.class)
@Override
protected ModelAndView handleBindException(
BindException ex, HttpServletRequest request, HttpServletResponse response, @Nullable Object handler)
throws IOException {
System.out.println("In CustomExceptionHandlerResolver");
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return new ModelAndView();
}

}

关于java - 如何自定义 DefaultHandlerExceptionResolver 逻辑?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53974000/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com