1
what is the right way to handle errors in spring-webfluxに提案されている3つのソリューションをすべて試しましたが、は呼び出されません。私はSpring Boot 2.0.0.M7
を使用しています。 GitHubのレポhereSpring 5 Reactive - WebExceptionHandlerが呼び出されない
@Configuration
class RoutesConfiguration {
@Autowired
private lateinit var testService: TestService
@Autowired
private lateinit var globalErrorHandler: GlobalErrorHandler
@Bean
fun routerFunction():
RouterFunction<ServerResponse> = router {
("/test").nest {
GET("/") {
ServerResponse.ok().body(testService.test())
}
}
}
}
@Component
class GlobalErrorHandler() : WebExceptionHandler {
companion object {
private val log = LoggerFactory.getLogger(GlobalErrorHandler::class.java)
}
override fun handle(exchange: ServerWebExchange?, ex: Throwable?): Mono<Void> {
log.info("inside handle")
/* Handle different exceptions here */
when(ex!!) {
is ClientException -> exchange!!.response.statusCode = HttpStatus.BAD_REQUEST
is Exception -> exchange!!.response.statusCode = HttpStatus.INTERNAL_SERVER_ERROR
}
return Mono.empty()
}
}
UPDATE:私は2.0.0.M2
に春のブートバージョンを変更すると、WebExceptionHandler
が
と呼ばなっています。 2.0.0.M7
のために何かする必要がありますか?
はSOLUTION:
ブライアンの提案を1として、それはそうでない場合は、エラーを処理するかもしれない、あなたがあなた自身のWebExceptionHandler
を提供することができますが、あなたは他の人に比較的それを注文する必要が
@Bean
@Order(-2)
fun globalErrorHandler() = GlobalErrorHandler()
Springブート組み込みの 'AbstractErrorWebExceptionHandler'は、Webページベースのアプリケーションのグローバルなエラー処理に適しています。 RESTアプリケーションの場合、httpステータスとエンティティを設定するソリューションのような '@ RestControllerAdvice'を提供する方が良いと思います。 – Hantsy