クラス RouterFunctions

java.lang.ObjectSE
org.springframework.web.servlet.function.RouterFunctions

public abstract class RouterFunctions extends ObjectSE
Spring の関数 Web フレームワークへの主要エントリポイント。発見可能なビルダースタイルの API を使用して RouterFunction を作成したり、RequestPredicate および HandlerFunction を指定して RouterFunction を作成したり、既存のルーティング関数でさらにサブルーチン化したりするなど、ルーティング機能を公開します。
導入:
5.2
作成者:
Arjen Poutsma, Sebastien Deleuze
  • フィールドの詳細

    • REQUEST_ATTRIBUTE

      public static final StringSE REQUEST_ATTRIBUTE
      ServerRequest を含むリクエスト属性の名前。
    • URI_TEMPLATE_VARIABLES_ATTRIBUTE

      public static final StringSE URI_TEMPLATE_VARIABLES_ATTRIBUTE
      変数名を値にマッピングする、URI テンプレートマップを含むリクエスト属性の名前。
    • MATCHING_PATTERN_ATTRIBUTE

      public static final StringSE MATCHING_PATTERN_ATTRIBUTE
      PathPattern として、一致するパターンを含むリクエスト属性の名前。
  • コンストラクターの詳細

    • RouterFunctions

      public RouterFunctions()
  • メソッドの詳細

    • route

      public static RouterFunctions.Builder route()
      ビルダースタイルのインターフェースを介してルーター関数を作成する発見可能な方法を提供します。
      戻り値:
      ルーター関数ビルダー
    • route

      public static <T extends ServerResponse> RouterFunction<T> route(RequestPredicate predicate, HandlerFunction<T> handlerFunction)
      指定されたリクエスト述語が適用される場合、指定されたハンドラー関数にルーティングします。

      たとえば、次の例では、"/user" の GET リクエストを userController の listUsers メソッドにルーティングします。

       RouterFunction<ServerResponse> route =
           RouterFunctions.route(RequestPredicates.GET("/user"), userController::listUsers);
       
      型パラメーター:
      T - ハンドラー関数によって返されるレスポンスの型
      パラメーター:
      predicate - テストする述語
      handlerFunction - 述語が適用される場合にルーティングするハンドラー関数
      戻り値:
      predicate が true と評価された場合に handlerFunction にルーティングするルーター関数
      関連事項:
    • nest

      public static <T extends ServerResponse> RouterFunction<T> nest(RequestPredicate predicate, RouterFunction<T> routerFunction)
      指定されたリクエスト述語が適用される場合、指定されたルーター関数にルーティングします。このメソッドを使用して、ネストされたルートを作成できます。ルートのグループは、共通のパス(プレフィックス)、ヘッダー、その他のリクエスト述語を共有します。

      たとえば、次の例では、最初に、GET の場合は listUsers に、POST の場合は createUser に解決される合成ルートを作成します。次に、この構成されたルートは "/user" パス述語でネストされるため、"/user" の GET リクエストはユーザーをリストし、"/user" の POST リクエストは新しいユーザーを作成します。

       RouterFunction<ServerResponse> userRoutes =
         RouterFunctions.route(RequestPredicates.method(HttpMethod.GET), this::listUsers)
           .andRoute(RequestPredicates.method(HttpMethod.POST), this::createUser);
       RouterFunction<ServerResponse> nestedRoute =
         RouterFunctions.nest(RequestPredicates.path("/user"), userRoutes);
       
      型パラメーター:
      T - ハンドラー関数によって返されるレスポンスの型
      パラメーター:
      predicate - テストする述語
      routerFunction - 述語が適用される場合に委譲するネストされたルーター関数
      戻り値:
      predicate が true と評価された場合に routerFunction にルーティングするルーター関数
      関連事項:
    • resource

      public static RouterFunction<ServerResponse> resource(RequestPredicate predicate, Resource resource)
      指定された述語に一致するリクエストを指定されたリソースにルーティングします。たとえば
       Resource resource = new ClassPathResource("static/index.html")
       RouterFunction<ServerResponse> resources = RouterFunctions.resource(path("/api/**").negate(), resource);
       
      パラメーター:
      predicate - 一致する述語
      resource - 提供するリソース
      戻り値:
      リソースにルーティングするルーター関数
      導入:
      6.1.4
    • resource

      public static RouterFunction<ServerResponse> resource(RequestPredicate predicate, Resource resource, BiConsumerSE<Resource,HttpHeaders> headersConsumer)
      指定された述語に一致するリクエストを指定されたリソースにルーティングします。たとえば
       Resource resource = new ClassPathResource("static/index.html")
       RouterFunction<ServerResponse> resources = RouterFunctions.resource(path("/api/**").negate(), resource);
       
      パラメーター:
      predicate - 一致する述語
      resource - 提供するリソース
      headersConsumer - 提供されるリソースの HTTP ヘッダーへのアクセスを提供します
      戻り値:
      リソースにルーティングするルーター関数
      導入:
      6.1.4
    • resources

      public static RouterFunction<ServerResponse> resources(StringSE pattern, Resource location)
      指定されたパターンに一致するリクエストを、指定されたルートの場所を基準にしたリソースにルーティングします。たとえば
       Resource location = new FileSystemResource("public-resources/");
       RouterFunction<ServerResponse> resources = RouterFunctions.resources("/resources/**", location);
       
      パラメーター:
      pattern - マッチングするパターン
      location - リソースを解決する必要のある場所のディレクトリ
      戻り値:
      リソースにルーティングするルーター関数
      関連事項:
    • resources

      public static RouterFunction<ServerResponse> resources(StringSE pattern, Resource location, BiConsumerSE<Resource,HttpHeaders> headersConsumer)
      指定されたパターンに一致するリクエストを、指定されたルートの場所を基準にしたリソースにルーティングします。たとえば
       Resource location = new FileSystemResource("public-resources/");
       RouterFunction<ServerResponse> resources = RouterFunctions.resources("/resources/**", location);
       
      パラメーター:
      pattern - マッチングするパターン
      location - リソースを解決する必要のある場所のディレクトリ
      headersConsumer - 提供されるリソースの HTTP ヘッダーへのアクセスを提供します
      戻り値:
      リソースにルーティングするルーター関数
      導入:
      6.1
      関連事項:
    • resourceLookupFunction

      public static FunctionSE<ServerRequest,OptionalSE<Resource>> resourceLookupFunction(StringSE pattern, Resource location)
      resources(String, Resource) で使用されるリソースルックアップ関数を返します。返される関数は、たとえば、ルックアップ関数が一致しない場合にデフォルトのリソースを返すように構成SEできます。
       Optional<Resource> defaultResource = Optional.of(new ClassPathResource("index.html"));
       Function<ServerRequest, Optional<Resource>> lookupFunction =
         RouterFunctions.resourceLookupFunction("/resources/**", new FileSystemResource("public-resources/"))
           .andThen(resource -> resource.or(() -> defaultResource));
       RouterFunction<ServerResponse> resources = RouterFunctions.resources(lookupFunction);
       
      パラメーター:
      pattern - マッチングするパターン
      location - リソースを解決する必要のある場所のディレクトリ
      戻り値:
      指定されたパラメーターのデフォルトのリソース検索関数。
      関連事項:
    • resources

      public static RouterFunction<ServerResponse> resources(FunctionSE<ServerRequest,OptionalSE<Resource>> lookupFunction)
      提供された検索機能を使用してリソースにルーティングします。ルックアップ関数が指定されたリクエストに Resource を提供する場合、GET、HEAD、OPTIONS リクエストを処理する HandlerFunction を使用して公開されます。
      パラメーター:
      lookupFunction - ServerRequest を指定して Resource を提供する関数
      戻り値:
      リソースにルーティングするルーター関数
    • resources

      public static RouterFunction<ServerResponse> resources(FunctionSE<ServerRequest,OptionalSE<Resource>> lookupFunction, BiConsumerSE<Resource,HttpHeaders> headersConsumer)
      提供された検索機能を使用してリソースにルーティングします。ルックアップ関数が指定されたリクエストに Resource を提供する場合、GET、HEAD、OPTIONS リクエストを処理する HandlerFunction を使用して公開されます。
      パラメーター:
      lookupFunction - ServerRequest を指定して Resource を提供する関数
      headersConsumer - 提供されるリソースの HTTP ヘッダーへのアクセスを提供します
      戻り値:
      リソースにルーティングするルーター関数
      導入:
      6.1
    • changeParser

      public static <T extends ServerResponse> RouterFunction<T> changeParser(RouterFunction<T> routerFunction, PathPatternParser parser)
      指定されたルーター関数PathPatternParser を変更します。このメソッドを使用すると、PathPatternParser プロパティをデフォルトから変更することができます (たとえば、大文字と小文字の区別を変更するなど)。
      型パラメーター:
      T - ハンドラー関数によって返されるレスポンスの型
      パラメーター:
      routerFunction - パーサーを変更するルーター関数
      parser - 変更するパーサー。
      戻り値:
      ルーター変更機能