在spring-webflux中处理错误的正确方法是什么 [英] what is the right way to handle errors in spring-webflux
问题描述
我一直在使用spring-webflux进行一些研究,我想了解使用路由器功能处理错误的正确方法.
I've been doing some research using spring-webflux and I like to understand what should be the right way to handle errors using Router Functions.
我创建了一个小项目来测试几个场景,我希望获得有关它的反馈,并查看其他人在做什么.
I've created an small project to test a couple of scenarios, and I like to get feedback about it, and see what other people is doing.
到目前为止,我在做什么.
So far what I doing is.
提供以下路由功能:
@Component
public class HelloRouter {
@Bean
RouterFunction<?> helloRouterFunction() {
HelloHandler handler = new HelloHandler();
ErrorHandler error = new ErrorHandler();
return nest(path("/hello"),
nest(accept(APPLICATION_JSON),
route(GET("/"), handler::defaultHello)
.andRoute(POST("/"), handler::postHello)
.andRoute(GET("/{name}"), handler::getHello)
)).andOther(route(RequestPredicates.all(), error::notFound));
}
}
我已经在我的处理程序上完成了
I've do this on my handler
class HelloHandler {
private ErrorHandler error;
private static final String DEFAULT_VALUE = "world";
HelloHandler() {
error = new ErrorHandler();
}
private Mono<ServerResponse> getResponse(String value) {
if (value.equals("")) {
return Mono.error(new InvalidParametersException("bad parameters"));
}
return ServerResponse.ok().body(Mono.just(new HelloResponse(value)), HelloResponse.class);
}
Mono<ServerResponse> defaultHello(ServerRequest request) {
return getResponse(DEFAULT_VALUE);
}
Mono<ServerResponse> getHello(ServerRequest request) {
return getResponse(request.pathVariable("name"));
}
Mono<ServerResponse> postHello(ServerRequest request) {
return request.bodyToMono(HelloRequest.class).flatMap(helloRequest -> getResponse(helloRequest.getName()))
.onErrorResume(error::badRequest);
}
}
我的错误处理程序会这样做:
Them my error handler do:
class ErrorHandler {
private static Logger logger = LoggerFactory.getLogger(ErrorHandler.class);
private static BiFunction<HttpStatus,String,Mono<ServerResponse>> response =
(status,value)-> ServerResponse.status(status).body(Mono.just(new ErrorResponse(value)),
ErrorResponse.class);
Mono<ServerResponse> notFound(ServerRequest request){
return response.apply(HttpStatus.NOT_FOUND, "not found");
}
Mono<ServerResponse> badRequest(Throwable error){
logger.error("error raised", error);
return response.apply(HttpStatus.BAD_REQUEST, error.getMessage());
}
}
这是完整的示例存储库:
Here is the full sample repo:
https://github.com/LearningByExample/reactive-ms-example
推荐答案
Spring 5 provides a WebHandler, and in the JavaDoc, there's the line:
使用HttpWebHandlerAdapter使WebHandler适应HttpHandler.WebHttpHandlerBuilder提供了一种方便的方法,同时还可以选择配置一个或多个过滤器和/或异常处理程序.
Use HttpWebHandlerAdapter to adapt a WebHandler to an HttpHandler. The WebHttpHandlerBuilder provides a convenient way to do that while also optionally configuring one or more filters and/or exception handlers.
当前,官方文档建议我们在启动任何服务器之前将路由器功能包装到HttpHandler中:
Currently, the official documentation suggests that we should wrap the router function into an HttpHandler before booting up any server:
HttpHandler httpHandler = RouterFunctions.toHttpHandler(routerFunction);
With the help of WebHttpHandlerBuilder, we can configure custom exception handlers:
HttpHandler httpHandler = WebHttpHandlerBuilder.webHandler(toHttpHandler(routerFunction))
.prependExceptionHandler((serverWebExchange, exception) -> {
/* custom handling goes here */
return null;
}).build();
这篇关于在spring-webflux中处理错误的正确方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!