We’re excited to launch the first post in our Spring Boot Technical Concepts series! We thought it was only appropriate to begin with Spring Boot Default Starters in Spring Boot.

Java Enterprise Edition in the late ’90s – especially Enterprise Java Beans (EJB), required an almost mystical knowledge of arcane XML descriptors and boilerplate code. In the early 2000’s, as EJB desperately tried to keep up with the emerging Java technologies, along came Spring.

It was like, well, like Spring had come to the endless winter of the enterprise Java landscape.

With minimal XML and a few POJOs (Plain Old Java Objects), you could now have a robust, enterprise-ready application. In conjunction with EJB alternatives like Hibernate, Spring rocketed to the forefront of enterprise Java development. At a time when interpreted functional language alternatives, like Ruby, were becoming popular, Java experienced a resurgence (arguably because of Spring) – even within smaller business applications.

Spring, in providing a major release just about every other year along with significant interim releases, has been a lot more agile than Java EE. (Almost 4 years passed between Java EE 6 and 7 releases.) Along the way, Spring has continuously improved. A major enhancement has been the ability to configure Spring in a completely declarative way with annotations relying on little to no XML at all.

Enter Spring Boot

On April 1, 2014, Spring Boot 1.0.0 was released. Spring Boot is Spring’s convention-over-configuration approach to rapidly developing, testing, running and deploying Spring Applications. Best of all, they can be just run from the command line without the need to deploy to a container.

The idea is that most Spring applications use a set of sensible defaults. Rather than explicitly specifying these defaults for every application, Spring Boot assumes them. They are easily overridden with Spring boot too.

Best of all, there’s a collection of Spring Boot Default Starters that adds features and functionality to your application with zero additional configuration on your part.

The Spring Initializr project, hosted at start.spring.io allows you to configure and download a complete Spring Boot project with just the libraries (starters) you need.

There are now 100 starters grouped into 19 categories. These range from Core starters to Web starters to a number of different categories of Cloud based starters.

Spring Boot Default Starters From Your Browser

Let’s create the most simple, barebones Spring Boot application with no other dependencies. Jump over to start.spring.io and click the Generate Project button:

Spring_Initializr

A file named demo.zip is downloaded.

Run the following from your terminal:

This will unpack, build and run this demo, which is pretty uneventful. All it does is fire up a Spring Boot application and exit. That’s because there are no starters configured at all. Let’s go back to start.spring.io, but this time, let’s add in the web starter.

Spring_Initializr1

Spring_Initializr2

This time, when you click the Generate Project button and run the above commands, you will see that an embedded Tomcat container starts and is listening on port 8080 (date/timestamps removed for brevity):

If you browse to this Spring Boot application, you will just get a 404 because no controllers have been configured, but at least you can see that you have a web application that is running and responding to requests.

localhost_8080

If you look at the pom.xml file in the demo folder, you’ll see that Spring Initializr has set things up for you, including the web dependency:

Spring Boot Default Starters From the Command Line

Are you already a denizen of the command line? Thanks to the brilliant content negotiation and meta data provided by Spring Initializr, you can interact with and create Spring Boot projects from http://start.spring.io using your favorite command line http client. The examples below use httpie.

First, just hit start.spring.io, and see what you get back:

This will give you a lot of output showing you all the possible parameters you can pass over to start.spring.io to configure your project.

Let’s replicate what we did earlier by creating a Spring Boot web enabled application.

The -d tells httpie to save the binary data coming through as the file named in the Content-Disposition header. The -v parameter tells httpie to give verbose output. Here’s what that looks like:

You now have a demo.zip file, just like before, that has everything you need to customize and fire up a Spring Boot web application.

Spring Boot Default Starters From Your IDE

Try this just for fun:

You’ll see that you get back a ton of JSON data. That’s because of the Accept:application/json header. This triggers a JSON response thanks to the built in content negotiation.

A few Java IDE’s have added the ability to create a Spring Boot project using Initializr. They do this exact thing behind the scenes: get all the meta-data from start.spring.io and then let you choose the options you want to add to your project right in the IDE.

IntelliJ Idea supports Spring Initializr. Follow these steps to see it in action:

  1. Choose: File -> New -> Project… from the menu.

  2. Select Spring Initializr and click Next button.

    New_Project

    Notice the Initializr Service URL (https://start.spring.io). We will circle back to that later.

  3. Customize your project and click the Next button.

    New_Project2

  4. Select the features you want in your Spring Boot project by clicking the check boxes. Below, you can see we’ve selected the Web starter, just as before. Click the Next Button.

    New_Project3

  5. Customize the project’s location on your machine and click the Finish button.

    New_Project4

You’ll be back in IntelliJ with your Spring Boot project all setup.

demo

Create and Host Your Own Spring Initializr

One of the greatest features of the Spring Initializr project is that it’s easily extensible and it’s as easy to host as any other Spring Boot application since it is, itself, a Spring Boot application.

Stormpath has created a number of Spring Boot Starters so that you can easily integrate Stormpath with your project. Let’s take a look at what it would take to make these starters available in Spring Initializr to make it even easier!

The first thing to do is to clone (or fork) the project here.

Next, we’ll add in information for the Stormpath Spring Boot Starters as a new Category called Identity Management. This is as easy as updating the initializr-service/application.yml file. Below is just the Stormpath additions to the dependencies section:

In the root of the project, we build Spring Initializr:

The easiest (although not the only) way to run the application is to install the Spring CLI tools. On Mac, you can do:

You can also install the tools with:

Once that’s done, switch into the initializr-service folder and run the app:

This fires up the Spring Boot application on the standar port, 8080.

Now, let’s go through the same exercise as before with IntelliJ, only this time, we will use our local Spring Initializr:

  1. Choose: File -> New -> Project… from the menu.

  2. Select Spring Initializr.

    This time, we’ll change the Initializr Service URL to: http://localhost:8080.

    New_Project5

    Click the Next button.

  3. Customize your project and click the Next button.

  4. Select the features you want in your Spring Boot project by clicking the check boxes. Below, you can see I’ve scrolled to the bottom and selected Stormpath WebMVC.

    New_Project7

    Click the Next Button.

  5. Customize the project’s location on your machine and click the Finish button.

You’ll be back in IntelliJ with your Spring Boot project all set up.

demo1

Above, you can see that the proper dependency has been added to the pom.xml file. Thank you, Spring Initializr!

What about hosting and deploying your customized Initializr? There’s a Spring CLI command for that!

This creates a runnable Spring Boot jar that you can run like any other self contained Spring Boot app.

Want to incorporate the latest version of the Stormpath Spring Boot Starters?

Head on over to: http://start.stormpath.io. This is a live deployment of a customized version of spring.start.io. It has all of the original Spring Boot Starters, as well as all the Stormpath starters.

Just like start.spring.io, you can use your browser, the command line, or the IntelliJ IDE.

Spring Boot starters super-charge your application and Spring Initializr makes it really easy to pick and choose the components you need to give your application a (configuration) head start. Go on over the start.spring.io (in your browser, on the command line, or from your IDE), check off the pile of tech you want to include in your application and start building!