This project provudes autoconfiguration and a CLI extension for Spring
Boot with Ratpack. Most of the features enabled here are actually
already in Ratpack, e.g. see @EnableRatpack
. Using this project as well
as ratpack-spring-boot
means lets you write an application using Ratpack
and Spring with slightly less overhead, just by adding dependencies.
@SpringBootApplication
public class Application {
@Bean
public Handler handler() {
return context -> context.render("Hello World");
}
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
If a single @Bean
of type Handler
is present it is installed and
serves the default route. For more routes you have to add @Beans
of
type Action<Chain>
and add handlers as needed to the chain inside
its execute method.
Ratpack has a Groovy DSL for building routes. Spring Boot CLI applications can take advantage of the nice syntax as follows.
This should run (as it is, with no imports):
ratpack {
handlers {
get {
render "Hello World"
}
}
}
Put it in a file called app.groovy
and run it like this:
$ spring run app.groovy
...
Then get the result in a browser at http://localhost:5050.
You can also use render json(...)
or render groovyTemplate(...)
features of Ratpack with no additional effort. Spring will pick up
Groovy templates by default from classpath:/templates
.
To use Spring effectively you will want to take advantage of the dependency injection and autoconfiguration features of Spring Boot as well. Here's a simple example with dependency injection and json:
@Service
class MyService {
String message() { "Hello World" }
}
ratpack {
handlers {
get { MyService service ->
render json([msg:service.message()])
}
}
}
The ratpack
DSL keyword only supports handlers
inside its
top-level closure (in non-Spring apps you might see bindings
as
well).
To run an application written in the Ratpack DSL you need the ratpack extensions to the Spring Boot CLI.
Since the Ratpack extensions are not part of the main Spring Boot feature set you will need to clone from this repo and build it locally, e.g.
$ git clone https://github.com/dsyer/spring-boot-ratpack
$ cd spring-boot-ratpack
$ mvn install
Once it is built, you can install the ratpack plugin. You either need to build Spring Boot or download a snapshot build of the CLI first, e.g.
$ mkdir -p /tmp/spring && cd $_
$ wget -O spring.tgz https://repo.spring.io/libs-snapshot-local/org/springframework/boot/spring-boot-cli/1.2.0.BUILD-SNAPSHOT/spring-boot-cli-1.2.0.BUILD-SNAPSHOT-bin.tar.gz
$ tar -zxf spring.tgz
The spring
CLI is now installed at /tmp/spring/spring-boot-cli-1.2.0.BUILD-SNAPSHOT
, so if you are a gvm user you can do this:
$ gvm install springboot ratpack /tmp/spring/spring-boot-cli-1.2.0.BUILD-SNAPSHOT
$ gvm use springboot ratpack
and then you can install the spring-boot-ratpack-cli
jar:
$ spring install org.springframework.boot:spring-boot-ratpack-cli:1.0.0.BUILD-SNAPSHOT