DefaultHandlerExceptionResolver

DefaultHandlerExceptionResolver

我想在Spring Boot应用程序中自定义DefaultHandlerExceptionResolver,但是发生异常时从未实现自定义实现。

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')
}

Application.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)(值越低优先级越高)。默认情况下,您的处理程序将具有比这些默认值低的优先级,因此不会调用它,因为默认值已经处理了异常。

您可以使用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();
    }

}

07-25 22:28