1. 概要

以前、RatpackとそのGoogleGuiceとの統合を導入しました。

この簡単な記事では、RatpackをSpring Bootと統合する方法を紹介します。

2. Mavenの依存関係

続行する前に、次の依存関係を pom.xmlに追加しましょう:

<dependency>
    <groupId>io.ratpack</groupId>
    <artifactId>ratpack-spring-boot-starter</artifactId>
    <version>1.4.6</version>
    <type>pom</type>
</dependency>

ratpack-spring-boot-starter pom依存関係は、ratpack-spring-bootspring-boot-starterを依存関係に自動的に追加します。

3. RatpackとSpringBootの統合

TomcatまたはUndertowが提供するサーブレットコンテナの代わりに、RatpackをSpringBootに埋め込むことができます。 Spring構成クラスに注釈を付ける必要があるのは @EnableRatpack タイプのBeanを宣言しますアクション

@SpringBootApplication
@EnableRatpack
public class EmbedRatpackApp {

    @Autowired 
    private Content content;
 
    @Autowired 
    private ArticleList list;

    @Bean
    public Action<Chain> home() {
        return chain -> chain.get(ctx -> ctx.render(content.body()));
    }

    public static void main(String[] args) {
        SpringApplication.run(EmbedRatpackApp.class, args);
    }
}

Spring Bootに精通している方は、 アクション Webフィルターおよび/またはコントローラーとして機能できます。

静的ファイルの提供に関しては、Ratpackは@Autowired ChainConfigurers/publicおよび/staticの下に静的リソースのハンドラーを自動的に登録します。

ただし、この「魔法」の現在の実装は、プロジェクトの設定と開発環境によって異なります。 したがって、さまざまな環境で静的リソースの安定した提供を実現するには、baseDirを明示的に指定する必要があります。

@Bean
public ServerConfig ratpackServerConfig() {
    return ServerConfig
      .builder()
      .findBaseDir("static")
      .build();
}

上記のコードは、クラスパスの下にstaticフォルダーがあることを前提としています。 また、ServerConfigBeanにratpackServerConfigという名前を付けて、RatpackConfigurationに登録されているデフォルトのBeanをオーバーライドします。

次に、ratpack-testを使用してアプリケーションをテストできます。

MainClassApplicationUnderTest appUnderTest
  = new MainClassApplicationUnderTest(EmbedRatpackApp.class);

@Test
public void whenSayHello_thenGotWelcomeMessage() {
    assertEquals("hello baeldung!", appUnderTest
      .getHttpClient()
      .getText("/hello"));
}

@Test
public void whenRequestList_thenGotArticles()  {
    assertEquals(3, appUnderTest
      .getHttpClient()
      .getText("/list")
      .split(",").length);
}

@Test
public void whenRequestStaticResource_thenGotStaticContent() {
    assertThat(appUnderTest
      .getHttpClient()
      .getText("/"), containsString("page is static"));
}

4. スプリングブーツとラットパックの統合

まず、必要なBeanをSpring構成クラスに登録します。

@Configuration
public class Config {

    @Bean
    public Content content() {
        return () -> "hello baeldung!";
    }
}

次に、 ratpack-spring-bootが提供する便利なメソッドspring(…)を使用して、レジストリを簡単に作成できます。

public class EmbedSpringBootApp {

    public static void main(String[] args) throws Exception {
        RatpackServer.start(server -> server
          .registry(spring(Config.class))
          .handlers(chain -> chain.get(ctx -> ctx.render(ctx
            .get(Content.class)
            .body()))));
    }
}

Ratpackでは、レジストリはリクエスト処理でのハンドラ間通信に使用されます。 ハンドラーで使用するContextオブジェクトは、Registryインターフェースを実装します。

ここでは、SpringBootによって提供されるListableBeanFactoryインスタンスがRegistryに適合され、HandlerContextでレジストリをバックアップします。 したがって、 Context から特定のタイプのオブジェクトを取得する場合は、ListableBeanFactoryを使用して一致するBeanを検索します。

5. 概要

このチュートリアルでは、SpringBootとRatpackを統合する方法について説明しました。

いつものように、完全な実装はGithub利用できます。