REST API: Rapid development with Spring Boot

A lot of frameworks for REST API development have come up over the years, covering different platforms and languages. For Java developers, there are a lot of choices, such as Jersey, Resteasy, Restlet and so on. But in this article, I’ll focus on Spring Framework, specifically it’s latest addition, Spring Boot, and how they can make your life easier for REST API development..

Spring needs no introduction, if you are experienced Java developer. If not, suffice it to say that it’s the most popular framework for web and enterprise Java development. What I especially like about Spring is the shear range of it’s features and the way they  easily integrate together. Seriously, it’s hard to find an area of enterprise application development that Spring does not have support for. Be it database access, message queues, social networks, chances are Spring has it covered.

Spring Boot is recent addition to Spring portfolio which enables dramatic increase of developer productivity. It’s main feature is that it comes already configured for most use cases. That mens  you can just concentrate on business logic and start building your API. In this article, I’ll focus on features which make Spring Boot great for REST API development. After that, I’ll cover some not so great parts of it.

REST API  meets Spring Boot

Ever sine it’s introduction in early 2000’s, REST architectural style has become the dominant approach to designing web services. As you can see from Google Trends graph bellow, REST has slowly surpassed SOAP as the preferred method of building web services

The reason REST became so popular is because it is based on open technologies and standards, such as HTTP. Unlike previously used technologies like SOAP, CORBA etc., it provides simple, lightweight interface which can be implemented by wide arrange of clients.  It is also independent of any particular language, server, network stack or vendor implementation. In fact, it provides a mean for seamless integration of various systems, only by exposing simple HTTP API.

Spring Boot features for REST API

Spring Boot is great tool for REST API development. Bellow is a list of some REST features and how Spring Bot fits into the picture related to them.

  • Simple, uniform interface – REST API provides interface based on URIs (Uniform Resource Identified) and HTTP methods. Spring Boot comes with awesome support for this, which allows unlimited flexibility and options to implement your API just the way you want
  • Content type negotiation – most commonly, REST API uses XML or JSON as data exchange format. Spring Boot comes with out-of-the-box support for data conversion between these formats and Java objects. In addition, it provides automatic selection and conversion based on required content type
  • Security – some REST endpoints need to be inaccessible to unauthorized clients. For these cases, Spring Security can be seamlessly integrated with Spring Boot. Spring Security supports various authentication and authorization options which can be used with any REST API
  • Caching – HTTP supports cache headers which allow clients to cache resources in order to improve performance. By using Spring Boot, you can leverage this feature and also powerful server side caching options, like described in this post about using caching in REST API
  • request status handling – in REST, HTTP status codes are used to convey information about success or failures of requests. With Spring Boot, you get automatic handling of success and failure of request processing,  since Spring Boot sets corresponding status codes based on processing results

This is in no way extensive list of Spring Boot features related to REST API development, but are ones most commonly used. Spring framework covers almost all aspects of server side application development, and you can use all of them with Spring Boot with almost zero configuration required.

Overview of Spring Boot

In essence, Spring Boot is an extension of Spring which comes auto-configuration support. What that means is that, when you create Spring Boot application, it is already configured with sensible values. Instead of wasting your time chasing configuration parameters. Spring Boot already did that for you. You can simply focus on application logic. The best part is, you don’t need any additional tools or code generation. Everything is done through annotations. For example, the code snippet bellow is a complete web application with REST endpoint:

Just like that, in like 10 lines of code, you get completely valid REST endpoint which prints “Hello, world” when invoked with GET request. When you build this code (using Maven or Gradle), the following happens:

  • Spring Boot configures Spring beans based on dependencies on classpath. This configuration is sensible for most use cases, but you can of course tweak it as needed
  • Automatically scans  application packages for Spring and Java configuration annotations
  • Boot creates executable JAR by bundling Tomcat instance which runs web application. If needed, you can use other containers instead of Tomcat, such as Jetty or Undertow

As you can see, Spring Boot can dramatically accelerate your flow of application development.  But, how does Boot do that? Read in the following sections.

Spring Boot starters

“Starters” are a way to manage dependencies in Spring Boot. You can think of them as modules that add functionality to your application. Each starter package contains code and configuration which can be added to your application instantly. For example, by adding starter packages, you get functionality  like database access, JPA, message queues integration etc.

When you add a starter package to your POM, you basically add JARs to a classpath. On application startup, Spring Boot will scan the classpath for dependencies, configuration annotations and configuration parameters. Then, it will configure each module with default configuration parameters which are specified by the starter. This dramatically improves speed of development, because you don’t have to waste time figuring what you need to configure and how.  You can see the full list of starters in Spring Boot documentation page.

For example, to have the code we showed in previous section run, we just need to add spring-boot-starter-web  to Maven POM file.

As I’ve said previously, each started comes with preconfigured defaults, but you can override these settings with your own values. In addition, you can create your own starter package for specific needs. This process is described in official Spring Boot documentation on creating custom starters.

Tools for development productivity

In addition to dependency management and coding goodies, Spring Boot also provides a set of tools to further boost productivity and code-test-debug cycle.

To get your project started quickly, Spring offers Spring Initializr.  It’s a web tool which can generate Maven POM or Gradle build file for your application. You specify some basic information, like project name, coordinates, main features you want in your application, and Initializr will generate project structure which you can  download and start working on immediately. The screenshot bellow show user interface of Spring Initializr:

rest api spring boot project initialize

As you can see, you can specify general features you want to support in your application, like JPA, NoSQL databases, message queue etc., and Spring Initializr will add correct dependencies to the POM. There is also another view which lists all supported faetures, and you can open it by clicking on “Switch to full version” link.

After you get started with the application, you will need a way to run and package it. For that purpose, Spring Boot provides a Maven plugin. By default, this plugin is included if your project inherits from Spring Boot POM. It is already configured to package the application as fat JAR with embedded Tomcat as web container. You can run this JAR directly from command line with simple command:

When you run this command in your terminal, Spring Boot application will start with the output similar to the image bellow:

rest api spring boot run output

Developer tools module

Another set of tools which Spring Boot provides can make your life even easier and further accelerate your speed of development. You can include spring-boot-devtools  as a dependency in your POM. Simply add the following snippet:

Including Spring Bot developer tools adds the following capabilities to your workflow:

  • additional properties to disable caching of certain resources, such as view templates. This way, when you change a view template, there’s no need to restart application, because your changes will be visible immediately
  • automatic restart when files on classpath change. For example, when you change some configuration parameter, your application will restart automatically to use new values
  • watching additional paths. By default, only classpath entries are monitored for changes. With developer tools, you can add external files or directories to be monitored by your application
  • LiveReload – this feature forces automatic reload of the browser page when resource is modified on file system

As you can see, including these tools can greatly improve your speed of development. Note that these tools are only used during development, and are excluded when your application is built for production. The reason is that, although they reduce development effort, they impact performance, so they are excluded from production build.

Testing support

Spring Framework comes with a set of tools which greatly improve writing test cases. You can easily write unit and integration tests by using dependency injection features. Once you specify dependencies in your beans, test running framework will automatically inject required dependencies and run the test. In this way, you can avoid writing a lot of boiler plate code.

For REST API testing specifically, testing framework provides extensive support for invoking endpoints with specific parameters and verifying results. Consider the code snippet bellow:

In this simple snippet, you specify HTTP POST request to certain endpoint, with certain content, headers and request attributes. You also specify that you expect HTTP status 201 to be returned from the endpoint. With few simple lines of code, you can test a lot of your REST API.

I find these kinds of tests to be extremely useful, and what’s most important, they are really quick to write. As best practices mandate high test coverage, this is a great way to test web layer of your application without tedious parts such as custom HTTP client code to invoke endpoints.

Spring Boot- the bad parts

This analysis would not be complete without mentioning some of the negative aspects of Spring Boot. As with any framework, there are some trade offs that need to be taken into account when consider Spring Boot for your next project.

The first thing that comes to mind is that Spring Boot is extremely opinionated. This is deliberate decision made by development team: in order to speed up things, you need to assume some common traits of most use cases. For most parts, I don’t think this is much of a problem, because you can easily customize project to your needs. But, there is a small percentage of cases when Spring Boot features get in the way and force you to do some ugly hacks and workarounds. For example, I had problems with the way Thymeleaf templates are assumed to be used with Spring Boot, and spent a lot of time figuring out how to implement them the way I wanted to.

Another thing that I find annoying as that Spring Boot wants your project to inherit it’s spring-boot-starter-parent  POM. From ease of development, this makes sense, because you are getting all Spring Boot magic automatically. But, sometimes you just can’t do this, for example if your company has unique parent POM you need to use. In that case, you can also use Spring Boot without parent POM, but it comes with a cost: you need to manually manage some dependencies and configure Spring Boot Maven plugin.

Finally, the output of Spring Boot is a fat JAR, ie. the JAR file that includes all the dependencies, resources, web server etc. Depending on application, this JAR can grow to hundreds of megabytes. But, for some use cases, this is not acceptable, because application needs to be deployed to web container as a WAR file. Fortunately, this is also possible because you can package Spring Boot application as WAR file and deploy it to Tomcat, Jetty or any other container.


I hope that this extensive analysis helped you see the benefit of using Spring Boot for REST API development. Although it has some shortcoming, I think the benefits strongly outweigh them. If you are Java developer, this is by far the best platform to base your REST API on. Not only that you get a boost in productivity, but you also get all the features of complete Spring portfolio for free.