Ratpack与Spring Boot的集成

2025/04/08

1. 概述

之前,我们介绍了Ratpack以及它与Google Guice的集成

在这篇简短的文章中,我们将展示如何将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与Spring Boot集成

我们可以将Ratpack嵌入到Spring Boot中,作为Tomcat或Undertow提供的Servlet容器的替代方案,我们只需要用@EnableRatpack标注一个Spring配置类,并声明Action<Chain>类型的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的人来说,Action<Chain>可以充当Web过滤器和/或控制器。

在提供静态文件时,Ratpack会在@Autowired ChainConfigurers中自动为/public和/static下的静态资源注册处理程序。

但是,目前这个“魔法”的实现依赖于我们的项目设置和开发环境。因此,如果我们要实现在不同环境中稳定提供静态资源,我们应该明确指定baseDir:

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

上述代码假设我们在classpath下有一个static文件夹;此外,我们将ServerConfig Bean命名为ratpackServerConfig以覆盖RatpackConfiguration中注册的默认Bean。

然后我们可以使用ratpack-test测试我们的应用程序:

MainClassApplicationUnderTest appUnderTest = new MainClassApplicationUnderTest(EmbedRatpackApp.class);

@Test
public void whenSayHello_thenGotWelcomeMessage() {
    assertEquals("hello tuyucheng!", 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. 将Spring Boot与Ratpack集成

首先,我们将在Spring配置类中注册所需的Bean:

@Configuration
public class Config {

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

然后我们可以使用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接口。

这里,Spring Boot提供的ListableBeanFactory实例适配到Registry,以支持Handler的Context中的注册器。因此,当我们想从Context中获取特定类型的对象时,将使用ListableBeanFactory来查找匹配的Bean。

5. 总结

在本教程中,我们探讨了如何集成Spring Boot和Ratpack。

Show Disqus Comments

Post Directory

扫码关注公众号:Taketoday
发送 290992
即可立即永久解锁本站全部文章