what is the right way to handle errors in spring-webflux

前端 未结 6 2075
轮回少年
轮回少年 2020-12-09 11:12

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 s

6条回答
  •  不知归路
    2020-12-09 11:48

    Spring 5 provides a WebHandler, and in the JavaDoc, there's the line:

    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.

    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();
    

提交回复
热议问题