本文介绍了Spring 5 Reactive-未调用WebExceptionHandler的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我已经尝试了什么是在spring-webflux中处理错误的正确方法,但是没有调用 WebExceptionHandler 。我正在使用 Spring Boot 2.0.0.M7 。 Github仓库此处I have tried all 3 solutions suggested in what is the right way to handle errors in spring-webflux, but WebExceptionHandler is not getting called. I am using Spring Boot 2.0.0.M7. Github repo here@Configurationclass 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()) } } }}@Componentclass 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() }} 更新:当我将Spring Boot版本更改为 2.0.0.M2 ,正在调用 WebExceptionHandler 。我需要为 2.0.0.M7 做些什么?When I change Spring Boot version to 2.0.0.M2, the WebExceptionHandler is getting called. Do I need to do something for 2.0.0.M7? 解决方案:按照Brian的建议,它的作用是As per Brian's suggestion, it worked as@Bean@Order(-2)fun globalErrorHandler() = GlobalErrorHandler()推荐答案您可以提供自己的 WebExceptionHandler ,但是您必须相对于其他用户订购它,否则他们可能会在您得到错误之前处理错误。机会尝试。You can provide your own WebExceptionHandler, but you have to order it relatively to others, otherwise they might handle the error before yours get a chance to try. Spring Boot提供的 DefaultErrorWebExceptionHandler (请参阅参考文档)在 -1 ResponseStatusExceptionHandler 在 0the DefaultErrorWebExceptionHandler provided by Spring Boot for error handling (see reference documentation) is ordered at -1the ResponseStatusExceptionHandler provided by Spring Framework is ordered at 0因此,您可以在错误处理组件上添加 @Order(-2),以便在现有组件之前进行订购。So you can add @Order(-2) on your error handling component, to order it before the existing ones. 这篇关于Spring 5 Reactive-未调用WebExceptionHandler的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-21 02:46