- Notifications
You must be signed in to change notification settings - Fork 41.6k
Closed
Description
Dear Spring Team,
Hope that you are doing very well.
I was reading your documentation to learn about exception handling in Reactive Web, and I tried to apply the example code that you provided in Kotlin but i faced several problems
I worked on my own to make it work, at the end I found out how to do it, and I thought it would be a good idea if you would like to update the code.
package com.saher.testing.exception import org.springframework.boot.autoconfigure.web.WebProperties import org.springframework.boot.autoconfigure.web.reactive.error.AbstractErrorWebExceptionHandler import org.springframework.boot.web.reactive.error.ErrorAttributes import org.springframework.context.ApplicationContext import org.springframework.http.HttpStatus import org.springframework.http.codec.ServerCodecConfigurer import org.springframework.stereotype.Component import org.springframework.web.reactive.function.server.* @Component class GlobalErrorWebExceptionHandler( errorAttributes: ErrorAttributes, resourceProperties: WebProperties.Resources, applicationContext: ApplicationContext, serverCodecConfigurer: ServerCodecConfigurer ) : AbstractErrorWebExceptionHandler( errorAttributes, resourceProperties, applicationContext ) { init { super.setMessageWriters(serverCodecConfigurer.writers) super.setMessageReaders(serverCodecConfigurer.readers) } override fun getRoutingFunction(errorAttributes: ErrorAttributes): RouterFunction<ServerResponse> { return coRouter { GET("/exception", ::exceptionHandler) } } private suspend fun exceptionHandler(serverRequest: ServerRequest): ServerResponse { return ServerResponse .status(HttpStatus.BAD_REQUEST) .bodyValueAndAwait( getError(serverRequest).localizedMessage ) } }
also you need to add this bean in the configuration
import org.springframework.boot.autoconfigure.web.WebProperties import org.springframework.context.annotation.Bean import org.springframework.stereotype.Component @Component class Resources { @Bean fun webResources(): WebProperties.Resources { return WebProperties.Resources() } }
I created a stackoverflow post for it over here
https://stackoverflow.com/questions/77383331/spring-webflux-reactive-error-handling-kotlin
Metadata
Metadata
Assignees
Labels
type: documentationA documentation updateA documentation update