Boot Your RESTful API Using Spring

With microservices being a popular approach on greenfield projects and equally used the liven up aging architectures, I wanted to explore what the Groovy / Spring world had to offer in this regard. Spring Boot is arguably the simplest way to get a lightweight RESTful service off the ground. In this post I will cover the basics on getting a HATEOASRESTful service that stores Person entities in a Mongo database.

What is HAL & HATEOAS?

HATEOAS (Hypermedia as the Engine of Application State) is a style of REST application architecture that allows a client to dynamically navigate the related resources through hypermedia links in the body of a response. HAL (Hypertext Application Language) defines a simple format to hyperlink between resources in your API, thus HAL can be used to implement HATEOAS.

Environment

Having stared at Maven xml files for many years I felt inclined to see how the other side lives and decided to take Gradle for a test drive. It did not take a lot of effort to get the build going but you do need to have a few obvious components installed before you can start.

Java

Make sure java is installed and on your path. I like to install it in /opt with a symlink current pointing to the version. Then stick current into your profile, this allows you to easily switch between versions by just changing the current symlink.

Bootstrapping the project

I always find it easier to tinker with something that is working when you are learning a new technology. Spring initializr provides a browser based wizard to generate a project skeleton. Make sure you select Gradle project with the following starters:

Rest Repositories

MongoDB

Actuator

Extract the generated zip archive in your project folder. The project's root folder will contain a set of Gradle wrapper scripts and jar file that provides a convenient way of bootstrapping your gradle build environment. It can be committed with the rest of the project files and will download and cache the version of Gradle used for the project.

To build the project run:

./gradlew

This might take a while the first time since it needs to download Gradle and all the projects dependencies but stay calm and don't panic, the second time will be much faster since all the dependencies are cached locally.

Hotswapping with Spring-Loaded

This might speed up your development cycle to a certain extent. It will reload classes that have changed without restarting the app. Unfortunately intellij does not recompile automatically, when a source file changes you have to force it to compile (SHIFT + COMMAND + F9 on Mac). Eclipse users don't have this problem.

Custom search queries like findByLastName will be available under /people/search/findByLastName{?name}.

Tweaks

Spring Boot supports configuration changes through properties or yaml files which can be activated through Spring Profiles. For example, to change the host that MongoDB run's on, you can edit application.properties or application-$profile.properties and add the following entry:

Conclusion

Most of this post was taken up by getting the environment configured, that aside, with Spring Boot you really do get a lot in return for your code. As this example shows, a full blown HATEOAS RESTful API with MongoDB backed storage for only a few lines of code by Java standards.