Ejemplo n.º 1
0
  /**
   * Convert the given {@linkplain RouterFunction routing function} into a {@link HttpHandler},
   * using the given strategies.
   *
   * <p>The returned {@code HttpHandler} can be adapted to run in
   *
   * <ul>
   *   <li>Servlet 3.1+ using the {@link
   *       org.springframework.http.server.reactive.ServletHttpHandlerAdapter},
   *   <li>Reactor using the {@link
   *       org.springframework.http.server.reactive.ReactorHttpHandlerAdapter},
   *   <li>RxNetty using the {@link
   *       org.springframework.http.server.reactive.RxNettyHttpHandlerAdapter}, or
   *   <li>Undertow using the {@link
   *       org.springframework.http.server.reactive.UndertowHttpHandlerAdapter}.
   * </ul>
   *
   * @param routerFunction the routing function to convert
   * @param strategies the strategies to use
   * @return an http handler that handles HTTP request using the given routing function
   */
  public static HttpHandler toHttpHandler(
      RouterFunction<?> routerFunction, HandlerStrategies strategies) {
    Assert.notNull(routerFunction, "RouterFunction must not be null");
    Assert.notNull(strategies, "HandlerStrategies must not be null");

    return new HttpWebHandlerAdapter(
        exchange -> {
          ServerRequest request = new DefaultServerRequest(exchange, strategies);
          addAttributes(exchange, request);
          HandlerFunction<?> handlerFunction = routerFunction.route(request).orElse(notFound());
          ServerResponse<?> response = handlerFunction.handle(request);
          return response.writeTo(exchange, strategies);
        });
  }
Ejemplo n.º 2
0
  /**
   * Converts the given {@linkplain RouterFunction routing function} into a {@link HttpHandler},
   * using the given configuration.
   *
   * <p>The returned {@code HttpHandler} can be adapted to run in
   *
   * <ul>
   *   <li>Servlet 3.1+ using the {@link
   *       org.springframework.http.server.reactive.ServletHttpHandlerAdapter},
   *   <li>Reactor using the {@link
   *       org.springframework.http.server.reactive.ReactorHttpHandlerAdapter},
   *   <li>RxNetty using the {@link
   *       org.springframework.http.server.reactive.RxNettyHttpHandlerAdapter}, or
   *   <li>Undertow using the {@link
   *       org.springframework.http.server.reactive.UndertowHttpHandlerAdapter}.
   * </ul>
   *
   * @param routerFunction the routing function to convert
   * @param configuration the configuration to use
   * @return an http handler that handles HTTP request using the given routing function
   */
  public static HttpHandler toHttpHandler(
      RouterFunction<?> routerFunction, Configuration configuration) {
    Assert.notNull(routerFunction, "'routerFunction' must not be null");
    Assert.notNull(configuration, "'configuration' must not be null");

    return new HttpWebHandlerAdapter(
        exchange -> {
          Request request = new DefaultRequest(exchange, configuration);
          addAttributes(exchange, request);

          HandlerFunction<?> handlerFunction = routerFunction.route(request).orElse(notFound());
          Response<?> response = handlerFunction.handle(request);
          return response.writeTo(exchange, configuration);
        });
  }