Creating custom ErrorWebExceptionHandler fails

前端 未结 3 1640
无人共我
无人共我 2021-02-19 18:46

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:

3条回答
  •  野的像风
    2021-02-19 19:23

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

提交回复
热议问题