Spring boot Spring 5被动-未调用WebExceptionHandler
我已尝试了中建议的所有3种解决方案,但未调用Spring boot Spring 5被动-未调用WebExceptionHandler,spring-boot,exception-handling,reactive-programming,spring-webflux,Spring Boot,Exception Handling,Reactive Programming,Spring Webflux,我已尝试了中建议的所有3种解决方案,但未调用WebExceptionHandler。我使用的是弹簧靴2.0.0.M7。Github回购 您可以提供自己的WebExceptionHandler,但您必须相对其他人订购,否则他们可能会在您有机会尝试之前处理错误 Spring Boot为错误处理()提供的DefaultErrorWebExceptionHandler在-1 Spring Framework提供的ResponseStatusExceptionHandler在0 因此,您可以在错误处理
WebExceptionHandler
。我使用的是弹簧靴2.0.0.M7
。Github回购
您可以提供自己的
WebExceptionHandler
,但您必须相对其他人订购,否则他们可能会在您有机会尝试之前处理错误
- Spring Boot为错误处理()提供的
在DefaultErrorWebExceptionHandler
-1
- Spring Framework提供的
在ResponseStatusExceptionHandler
0
因此,您可以在错误处理组件上添加
@Order(-2)
,以便在现有组件之前对其进行排序。您可以提供自己的WebExceptionHandler
,但您必须相对其他组件进行排序,否则他们可能会在您有机会尝试之前处理错误
- Spring Boot为错误处理()提供的
在DefaultErrorWebExceptionHandler
-1
- Spring Framework提供的
在ResponseStatusExceptionHandler
0
因此,您可以在错误处理组件上添加
@Order(-2)
,以便在现有组件之前对其进行排序。错误响应应具有标准有效负载信息。这可以通过扩展AbstractErrorWebExceptionHandler
错误响应:数据类
data class ErrorResponse(
val timestamp: String,
val path: String,
val status: Int,
val error: String,
val message: String
)
ServerResponseBuilder:构建错误响应的两种不同方法
- 默认值:处理标准错误
- webClient:处理
异常(webClient
),不适用于这种情况WebClientResponseException
classserverresponsebuilder( 私有val请求:ServerRequest, 专用val状态:HttpStatus){ fun default():Mono= 服务器响应 .地位(地位) .body(BodyInserters.fromObject)(错误响应( 日期().format(), request.path(), status.value(), status.name, 状态(短语) 有趣的网络客户端(e:webClient响应异常):Mono= 服务器响应 .地位(地位) .body(BodyInserters.fromObject)(错误响应( 日期().format(), request.path(), e、 statusCode.value(), e、 message.toString(), e、 responseBodyAsString))) }
@Configuration
@Order(-2)
class GlobalErrorHandlerConfiguration @Autowired constructor(
errorAttributes: ErrorAttributes,
resourceProperties: ResourceProperties,
applicationContext: ApplicationContext,
viewResolversProvider: ObjectProvider<List<ViewResolver>>,
serverCodecConfigurer: ServerCodecConfigurer) :
AbstractErrorWebExceptionHandler(
errorAttributes,
resourceProperties,
applicationContext
) {
init {
setViewResolvers(viewResolversProvider.getIfAvailable { emptyList() })
setMessageWriters(serverCodecConfigurer.writers)
setMessageReaders(serverCodecConfigurer.readers)
}
override fun getRoutingFunction(errorAttributes: ErrorAttributes?): RouterFunction<ServerResponse> =
RouterFunctions.route(RequestPredicates.all(), HandlerFunction<ServerResponse> { response(it, errorAttributes) })
private fun response(request: ServerRequest, errorAttributes: ErrorAttributes?): Mono<ServerResponse> =
ServerResponseBuilder(request, status(request, errorAttributes)).default()
private fun status(request: ServerRequest, errorAttributes: ErrorAttributes?) =
HttpStatus.valueOf(errorAttributesMap(request, errorAttributes)["status"] as Int)
private fun errorAttributesMap(request: ServerRequest, errorAttributes: ErrorAttributes?) =
errorAttributes!!.getErrorAttributes(request, false)
}
@配置
@订单(-2)
类GlobalErrorHandlerConfiguration@Autowired构造函数(
errorAttributes:errorAttributes,
resourceProperties:resourceProperties,
applicationContext:applicationContext,
viewResolversProvider:ObjectProvider,
ServerCodeConfigurer:ServerCodeConfigurer):
AbstractErrorWebExceptionHandler(
错误属性,
资源属性,
应用程序上下文
) {
初始化{
SetViewResolver(viewResolversProvider.getIfAvailable{emptyList()})
SetMessageWriter(ServerCodeConfigurer.Writer)
setMessageReaders(ServerCodeConfigurer.readers)
}
重写fun getRoutingFunction(errorAttributes:errorAttributes?):RouterFunction=
RouterFunctions.route(RequestPredicates.all(),HandlerFunction{response(it,errorAttributes)})
私人娱乐响应(请求:ServerRequest,errorAttributes:errorAttributes?):Mono=
ServerResponseBuilder(请求,状态(请求,错误属性)).default()
私人娱乐状态(请求:ServerRequest,errorAttributes:errorAttributes?)=
HttpStatus.valueOf(errorAttributesMap(请求,errorAttributes)[“状态”]为Int)
private fun errorAttributesMap(请求:ServerRequest,errorAttributes:errorAttributes?)=
errorAttributes!!.getErrorAttributes(请求,false)
}
错误响应应具有标准有效负载信息。这可以通过扩展AbstractErrorWebExceptionHandler
错误响应:数据类
data class ErrorResponse(
val timestamp: String,
val path: String,
val status: Int,
val error: String,
val message: String
)
ServerResponseBuilder:构建错误响应的两种不同方法
- 默认值:处理标准错误
- webClient:处理
异常(webClient
),不适用于这种情况WebClientResponseException
classserverresponsebuilder( 私有val请求:ServerRequest, 专用val状态:HttpStatus){ fun default():Mono= 服务器响应 .地位(地位) .body(BodyInserters.fromObject)(错误响应( 日期().format(), request.path(), status.value(), status.name, 状态(短语) 有趣的网络客户端(e:webClient响应异常):Mono= 服务器响应 .地位(地位) .body(BodyInserters.fromObject)(错误响应( 日期().format(), request.path(), e、 statusCode.value(), e、 message.toString(), e、 responseBodyAsString))) }
@Configuration
@Order(-2)
class GlobalErrorHandlerConfiguration @Autowired constructor(
errorAttributes: ErrorAttributes,
resourceProperties: ResourceProperties,
applicationContext: ApplicationContext,
viewResolversProvider: ObjectProvider<List<ViewResolver>>,
serverCodecConfigurer: ServerCodecConfigurer) :
AbstractErrorWebExceptionHandler(
errorAttributes,
resourceProperties,
applicationContext
) {
init {
setViewResolvers(viewResolversProvider.getIfAvailable { emptyList() })
setMessageWriters(serverCodecConfigurer.writers)
setMessageReaders(serverCodecConfigurer.readers)
}
override fun getRoutingFunction(errorAttributes: ErrorAttributes?): RouterFunction<ServerResponse> =
RouterFunctions.route(RequestPredicates.all(), HandlerFunction<ServerResponse> { response(it, errorAttributes) })
private fun response(request: ServerRequest, errorAttributes: ErrorAttributes?): Mono<ServerResponse> =
ServerResponseBuilder(request, status(request, errorAttributes)).default()
private fun status(request: ServerRequest, errorAttributes: ErrorAttributes?) =
HttpStatus.valueOf(errorAttributesMap(request, errorAttributes)["status"] as Int)
private fun errorAttributesMap(request: ServerRequest, errorAttributes: ErrorAttributes?) =
errorAttributes!!.getErrorAttributes(request, false)
}
@配置
@订单(-2)
类GlobalErrorHandlerConfiguration@Autowired构造函数(
errorAttributes:errorAttributes,
resourceProperties:resourceProperties,
applicationContext:applicationContext,
viewResolversProvider:ObjectProvider,
ServerCodeConfigurer:ServerCodeConfigurer):
A.
@Configuration
@Order(-2)
class GlobalErrorHandlerConfiguration @Autowired constructor(
errorAttributes: ErrorAttributes,
resourceProperties: ResourceProperties,
applicationContext: ApplicationContext,
viewResolversProvider: ObjectProvider<List<ViewResolver>>,
serverCodecConfigurer: ServerCodecConfigurer) :
AbstractErrorWebExceptionHandler(
errorAttributes,
resourceProperties,
applicationContext
) {
init {
setViewResolvers(viewResolversProvider.getIfAvailable { emptyList() })
setMessageWriters(serverCodecConfigurer.writers)
setMessageReaders(serverCodecConfigurer.readers)
}
override fun getRoutingFunction(errorAttributes: ErrorAttributes?): RouterFunction<ServerResponse> =
RouterFunctions.route(RequestPredicates.all(), HandlerFunction<ServerResponse> { response(it, errorAttributes) })
private fun response(request: ServerRequest, errorAttributes: ErrorAttributes?): Mono<ServerResponse> =
ServerResponseBuilder(request, status(request, errorAttributes)).default()
private fun status(request: ServerRequest, errorAttributes: ErrorAttributes?) =
HttpStatus.valueOf(errorAttributesMap(request, errorAttributes)["status"] as Int)
private fun errorAttributesMap(request: ServerRequest, errorAttributes: ErrorAttributes?) =
errorAttributes!!.getErrorAttributes(request, false)
}