1. Overview
1.概述
Previously, we have introduced Ratpack and its integration with Google Guice.
在此之前,我们已经介绍了Ratpack及其与Google Guice的集成。
In this quick article, we’ll show how Ratpack can be integrated with Spring Boot.
在这篇快速文章中,我们将展示Ratpack如何与Spring Boot集成。
2. Maven Dependency
2.Maven的依赖性
Before we continue, let’s add the following dependency into our pom.xml:
在我们继续之前,让我们在我们的pom.xml中添加以下依赖性:。
<dependency>
<groupId>io.ratpack</groupId>
<artifactId>ratpack-spring-boot-starter</artifactId>
<version>1.4.6</version>
<type>pom</type>
</dependency>
The ratpack-spring-boot-starter pom dependency automatically adds ratpack-spring-boot and spring-boot-starter into our dependencies.
ratpack-spring-boot-starter pom依赖项自动将ratpack-spring-boot和spring-boot-starter纳入我们的依赖项。
3. Integrating Ratpack With Spring Boot
3.将Ratpack与Spring Boot结合起来
We can embed Ratpack in Spring Boot as an alternative to servlet containers provided by Tomcat or Undertow. We only need to annotate a Spring configuration class with @EnableRatpack and declare beans of type Action<Chain>:
我们可以在Spring Boot中嵌入Ratpack,作为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);
}
}
For those more familiar with Spring Boot, the Action<Chain> can act as a web filter and/or controller.
对于那些更熟悉Spring Boot的人来说,Action<Chain>可以作为一个网络过滤器和/或控制器。
When it comes to serving static files, Ratpack registers handlers automatically for static resources under /public and /static in the @Autowired ChainConfigurers.
当涉及到为静态文件提供服务时,Ratpack在@Autowired ChainConfigurers中为/public和/static下的静态资源自动注册了处理程序。
However, the current implementation of this “magic” depends on our project settings and development environment. So if we are to achieve stable serving of static resources in different environments, we should specify the baseDir explicitly:
然而,这个 “魔法 “的当前实现取决于我们的项目设置和开发环境。因此,如果我们要在不同的环境中实现静态资源的稳定服务,我们应该明确指定baseDir。
@Bean
public ServerConfig ratpackServerConfig() {
return ServerConfig
.builder()
.findBaseDir("static")
.build();
}
The code above assumes that we have a static folder under classpath. Also, we name the ServerConfig bean to ratpackServerConfig to override the default bean registered in RatpackConfiguration.
上面的代码假定我们在classpath下有一个static文件夹。此外,我们将ServerConfig Bean命名为ratpackServerConfig,以覆盖RatpackConfiguration中注册的默认bean。
Then we can test our application with ratpack-test:
然后我们可以用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. Integrating Spring Boot With Ratpack
4.将Spring Boot与Ratpack整合在一起
First, we will register required beans in a Spring configuration class:
首先,我们将在Spring配置类中注册所需的bean。
@Configuration
public class Config {
@Bean
public Content content() {
return () -> "hello baeldung!";
}
}
Then we can create a registry easily using the convenience method spring(…) provided by ratpack-spring-boot:
然后我们可以使用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()))));
}
}
In Ratpack, registries are used for inter-handler communication in request processing. The Context object that we use in handlers implements the Registry interface.
在Ratpack中,注册表被用于请求处理中的处理程序之间的通信。我们在处理程序中使用的Context对象,实现了Registry接口。
Here, a ListableBeanFactory instance provided by Spring Boot is adapted to a Registry, to back the registry in Handler‘s Context. Thus when we want to get an object of a specific type from Context, the ListableBeanFactory will be used to look up the matching beans.
这里,Spring Boot提供的ListableBeanFactory实例与Registry相适应,以支持Handler的Context中的注册表。因此,当我们想从Context中获得一个特定类型的对象时,ListableBeanFactory将被用来查找匹配的Bean。
5. Summary
5.总结
In this tutorial, we explored how to integrate Spring Boot and Ratpack.
在本教程中,我们探讨了如何整合Spring Boot和Ratpack。
As always, the full implementation is available over on Github.
一如既往,完整的实施方案可在Github上获得,。