Creating custom ErrorWebExceptionHandler fails

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());
}

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.


I just did this as well, and after looking at springs implementation I just added the components to the constructor.

@Component
@Order(-2)
class GlobalErrorWebExceptionHandler(
        errorAttributes: ErrorAttributes,
        resourceProperties: ResourceProperties,
        applicationContext: ApplicationContext,
        viewResolvers: ObjectProvider<ViewResolver>,
        serverCodecConfigurer: ServerCodecConfigurer
) : AbstractErrorWebExceptionHandler(
        errorAttributes,
        resourceProperties,
        applicationContext
) {
    private val logger = LogFactory.getLog(GlobalErrorWebExceptionHandler::class.java)!!

    init {
        setViewResolvers(viewResolvers.orderedStream().collect(Collectors.toList()))
        setMessageWriters(serverCodecConfigurer.writers)
        setMessageReaders(serverCodecConfigurer.readers)
    }

    override fun getRoutingFunction(errorAttributes: ErrorAttributes) = RouterFunctions.route(RequestPredicates.all(), HandlerFunction { request ->
        val ex = getError(request)
        logger.error(ex.message)

        ServerResponse.ok().build()
    })
}