问题
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