When we want to transform a
Promise value we can use the
flatMap methods. There are also variants to this methods that will only transform a value when a given predicate is true:
flatMapIf. We provide a predicate and function to the methods. If the predicate is true the function is invoked, otherwise the function is not invoked and the promised value is returned as is.
In a previous post we saw how we can use Spring Boot in a Ratpack application. But the integration can also be the other way around: using Ratpack in a Spring Boot application. This way we can use Ratpack’s power to handle requests sent to our Spring Boot application and still use all Spring Boot features in our application. The easiest way to add Ratpack to a Spring Boot application is adding a Ratpack dependency and use the
@EnableRatpack annotation. With this annotation a
RatpackServer instance is created and started along with configuration options.
Testing a Ratpack application is not difficult. Ratpack has excellent support for writing unit and integration tests. When we create the fixture
MainClassApplicationUnderTest we can override the method
addImpositions to add mock objects to the application. We can add them using the
ImpositionsSpec object. When the application starts with the test fixture the provided mock objects are used instead of the original objects. For a Groovy based Ratpack application we can do the same thing when we create the fixture
We have several options to define a Ratpack application. We can use a Java syntax to set up the bindings and handlers. Or we can use the very nice Groovy DSL. It turns out we can use both together as well. For example we can define the handlers with the Groovy DSL and the rest of the application definition is written in Java. To combine both we start with the Java configuration and use the
handlers method of the
Groovy.Script class to inject the files with the Groovy DSL.
One of the very nice features of Ratpack is the Groovy DSL to define our application. We get a nice DSL to set up the registry, to define handlers and more. Because of clever use of the
@DelegateTo annotation we get good code completion in our IDE. We can also add static compilation of our Groovy DSL when we start our Ratpack application. With static compilation the script is type checked at compile time so we get earlier feedback on possible errors in the script. To configure static compilation we must invoke the
app method of the
Groovy.Script class with the argument
Ratpack uses renderers to render output. We can create our own renderer by implementing the
Renderer interface. The renderer class needs to implement a
render method that has the object we want to render as argument. Alternatively we can add the logic to render a object to the class definition of that object. So instead of having a separate renderer class for a class, we add the render logic to the class itself. To achieve this we must implement the
Renderable interface for our class. Ratpack provides a
RenderableRenderer in the registry that knows how to render classes that implement the
Ratpack has parsers to parse a request with a JSON body or a HTML form. We simply use the
parse method of
Context and Ratpack will check if there is a compliant parser in the registry. If there is a parser for that type available then Ratpack will parse the request and return a
Promise with the value. To write a new parser we need to implement the
Parser interface. The easiest way to implement this interface is by writing a class that extends
ParserSupport. Using the
ParserSupport class we can also work with an options object that a user can pass on to the
parse method of
Context. If we don’t need options we can also extend the
In a previous post we learned about Spring Cloud Contract. We saw how we can use contracts to implement the server side of the contract. But Spring Cloud Contract also creates a stub based on the contract. The stub server is implemented with Wiremock and Spring Boot. The server can match incoming requests with the contracts and send back the response as defined in the contract. Let’s write an application that is invoking HTTP requests on the server application we wrote before. In the tests that we write for this client application we use the stub that is generated by Spring Cloud Contract. We know the stub is following the contract of the actual server.
Spring Cloud Contract is a project that allows to write a contract for a service using a Groovy DSL. In the contract we describe the expected requests and responses for the service. From this contract a stub is generated that can be used by a client application to test the code that invokes the service. Spring Cloud Contract also generates tests based on the contract for the service implementation. Let’s see how we can use the generated tests for the service implementation for a Ratpack application.
When we need to create a
URI object in Ratpack we can use the
HttpUrlBuilder class. We use several methods to build up a complete
URI object in an easy way. This is very useful when we for example use Ratpack’s
HttpClient object and we need to pass an
URI to do a request.