首页 > 解决方案 > 创建自定义 ErrorWebExceptionHandler 失败

问题描述

我试图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]

我的处理程序(Kotlin 代码):

@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
    }
}

可能是什么原因?

标签: spring-bootspring-webflux

解决方案


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

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

推荐阅读