Creating custom ErrorWebExceptionHandler fails

本秂侑毒 提交于 2020-04-09 18:09:29

问题


I am trying to create my own ErrorWebExceptionHandler in Spring Boot 2 by extending the default one but my application fails to start with the following message:

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]

My handler (Kotlin code):

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

What could be the cause?


回答1:


You need to set the messageWriters on that instance, because they are required here. You should probably create that as a @Bean, just like Spring Boot is doing in the dedicated auto-configuration.




回答2:


Please try to add dependency of ServerCodecConfigurer in your constructor

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



来源:https://stackoverflow.com/questions/52497901/creating-custom-errorwebexceptionhandler-fails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!