创建自定义ErrorWebExceptionHandler失败

Mil*_*nda 5 spring-boot spring-webflux

我试图ErrorWebExceptionHandler通过扩展默认启动项在Spring Boot 2中创建自己的启动项,但是我的应用程序无法以以下消息启动:

Caused by: java.lang.IllegalArgumentException: Property 'messageWriters' is required
at org.springframework.boot.autoconfigure.web.reactive.error.AbstractErrorWebExceptionHandler.afterPropertiesSet(AbstractErrorWebExceptionHandler.java:214) ~[spring-boot-autoconfigure-2.0.4.RELEASE.jar:2.0.4.RELEASE]
Run Code Online (Sandbox Code Playgroud)

我的处理程序(科特琳代码):

@Component
@Order(-2)
class SampleErrorWebExceptionHandler(
    errorAttributes: ErrorAttributes?,
    resourceProperties: ResourceProperties?,
    errorProperties: ErrorProperties?,
    applicationContext: ApplicationContext?
) : DefaultErrorWebExceptionHandler(errorAttributes, resourceProperties, errorProperties, applicationContext) {

    override fun logError(request: ServerRequest, errorStatus: HttpStatus) {
        // do something
    }
}
Run Code Online (Sandbox Code Playgroud)

可能是什么原因?

Bri*_*zel 5

您需要messageWriters在该实例上设置on,因为这里需要它们。您可能应该将其创建为a @Bean,就像Spring Boot在专用自动配置中所做的那样

  • 我已经添加了对ServerCodecConfigurer的依赖,从那里我可以获取`messageWriters`和`messageReaders`,并且它现在可以工作了,谢谢! (2认同)

sha*_*ari 5

请尝试在构造函数中添加ServerCodecConfigurer的依赖项

GlobalErrorWebExceptionHandler(ErrorAttributes errorAttributes, ResourceProperties resourceProperties, ApplicationContext applicationContext, ServerCodecConfigurer configurer) { super(errorAttributes, resourceProperties, applicationContext); this.setMessageWriters(configurer.getWriters()); }