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:

A file named demo.zip is downloaded.

Run the following from your terminal:

unzip demo.zip && \ cd demo && \ mvn clean install && \ java -jar target/*.jar 1 2 3 4 5 unzip demo . zip && \ cd demo && \ mvn clean install && \ java - jar target /* . jar

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.

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):

s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http) com.example.DemoApplication : Started DemoApplication in 2.959 seconds (JVM running for 3.318) o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet' o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 35 ms 1 2 3 4 5 6 s . b . c . e . t . TomcatEmbeddedServletContainer : Tomcat started on port ( s ) : 8080 ( http ) com . example . DemoApplication : Started DemoApplication in 2.959 seconds ( JVM running for 3.318 ) o . a . c . c . C . [ Tomcat ] . [ localhost ] . [ / ] : Initializing Spring FrameworkServlet 'dispatcherServlet' o . s . web . servlet . DispatcherServlet : FrameworkServlet 'dispatcherServlet' : initialization started o . s . web . servlet . DispatcherServlet : FrameworkServlet 'dispatcherServlet' : initialization completed in 35 ms

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.

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:

... <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> ... 1 2 3 4 5 6 7 . . . < dependency > < groupId > org . springframework . boot < / groupId > < artifactId > spring - boot - starter - web < / artifactId > < / 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:

http start.spring.io 1 2 http start . spring . io

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.

http -v start.spring.io/starter.zip dependencies=web baseDir=demo -d 1 2 http - v start . spring . io / starter . zip dependencies = web baseDir = demo - d

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:

POST /starter.zip HTTP/1.1 Accept: application/json Connection: keep-alive Content-Length: 42 Content-Type: application/json Host: start.spring.io User-Agent: HTTPie/0.9.3 { "baseDir": "demo", "dependencies": "web" } HTTP/1.1 200 OK CF-RAY: 2be5911b2bc11840-EWR Connection: keep-alive Content-Disposition: attachment; filename="demo.zip" Content-Length: 50956 Content-Type: application/zip;charset=UTF-8 Date: Wed, 06 Jul 2016 19:47:08 GMT Server: cloudflare-nginx Set-Cookie: __cfduid=d1e5718ac8125bcb1930bde5ea2a343a61467834428; expires=Thu, 06-Jul-17 19:47:08 GMT; path=/; domain=.spring.io; HttpOnly X-Application-Context: start:cloud:1 X-Vcap-Request-Id: 1b6e59b5-6717-4e55-61a0-c49ba2e25c23 Downloading 49.76 kB to "demo.zip" Done. 49.76 kB in 0.02079s (2.34 MB/s) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 POST / starter . zip HTTP / 1.1 Accept : application / json Connection : keep - alive Content - Length : 42 Content - Type : application / json Host : start . spring . io User - Agent : HTTPie / 0.9.3 { "baseDir" : "demo" , "dependencies" : "web" } HTTP / 1.1 200 OK CF - RAY : 2be5911b2bc11840 - EWR Connection : keep - alive Content - Disposition : attachment ; filename = "demo.zip" Content - Length : 50956 Content - Type : application / zip ; charset = UTF - 8 Date : Wed , 06 Jul 2016 19 : 47 : 08 GMT Server : cloudflare - nginx Set - Cookie : __cfduid = d1e5718ac8125bcb1930bde5ea2a343a61467834428 ; expires = Thu , 06 - Jul - 17 19 : 47 : 08 GMT ; path =/ ; domain = . spring . io ; HttpOnly X - Application - Context : start : cloud : 1 X - Vcap - Request - Id : 1b6e59b5 - 6717 - 4e55 - 61a0 - c49ba2e25c23 Downloading 49.76 kB to "demo.zip" Done . 49.76 kB in 0.02079s ( 2.34 MB / s )

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:

http start.spring.io Accept:application/json 1 2 http start . spring . io Accept : application / json

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:

Choose: File -> New -> Project… from the menu. Select Spring Initializr and click Next button. Notice the Initializr Service URL (https://start.spring.io). We will circle back to that later. Customize your project and click the Next button. 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. 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 setup.

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:

... - name: Identity Management content: - name: Stormpath Base groupId: com.stormpath.spring artifactId: stormpath-spring-boot-starter id: stormpath-spring-boot-starter description: Spring Boot Starter for Stormpath version: 1.0.RC9.2 - name: Stormpath WebMVC groupId: com.stormpath.spring artifactId: stormpath-webmvc-spring-boot-starter id: stormpath-webmvc-spring-boot-starter description: Stormpath WebMVC Spring Boot Starter (includes Base) version: 1.0.RC9.2 - name: Stormpath Thymeleaf groupId: com.stormpath.spring artifactId: stormpath-thymeleaf-spring-boot-starter id: stormpath-thymeleaf-spring-boot-starter description: Spring Boot WebMVC Starter for Stormpath with Thymeleaf views version: 1.0.RC9.2 - name: Stormpath Spring Security groupId: com.stormpath.spring artifactId: stormpath-spring-security-spring-boot-starter id: stormpath-spring-security description: Spring Boot Starter for Stormpath with Spring Security Integration version: 1.0.RC9.2 - name: Stormpath Spring Security WebMVC groupId: com.stormpath.spring artifactId: stormpath-spring-security-webmvc-spring-boot-starter id: stormpath-spring-security-webmvc-spring-boot-starter description: Spring Boot WebMVC Starter for Stormpath with Spring Security version: 1.0.RC9.2 - name: Stormpath Default groupId: com.stormpath.spring artifactId: stormpath-default-spring-boot-starter id: stormpath-default-spring-boot-starter description: Spring Boot Default Starter for Stormpath with WebMVC, Thymeleaf and Spring Security version: 1.0.RC9.2 ... 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 . . . - name : Identity Management content : - name : Stormpath Base groupId : com . stormpath . spring artifactId : stormpath - spring - boot - starter id : stormpath - spring - boot - starter description : Spring Boot Starter for Stormpath version : 1.0.RC9.2 - name : Stormpath WebMVC groupId : com . stormpath . spring artifactId : stormpath - webmvc - spring - boot - starter id : stormpath - webmvc - spring - boot - starter description : Stormpath WebMVC Spring Boot Starter ( includes Base ) version : 1.0.RC9.2 - name : Stormpath Thymeleaf groupId : com . stormpath . spring artifactId : stormpath - thymeleaf - spring - boot - starter id : stormpath - thymeleaf - spring - boot - starter description : Spring Boot WebMVC Starter for Stormpath with Thymeleaf views version : 1.0.RC9.2 - name : Stormpath Spring Security groupId : com . stormpath . spring artifactId : stormpath - spring - security - spring - boot - starter id : stormpath - spring - security description : Spring Boot Starter for Stormpath with Spring Security Integration version : 1.0.RC9.2 - name : Stormpath Spring Security WebMVC groupId : com . stormpath . spring artifactId : stormpath - spring - security - webmvc - spring - boot - starter id : stormpath - spring - security - webmvc - spring - boot - starter description : Spring Boot WebMVC Starter for Stormpath with Spring Security version : 1.0.RC9.2 - name : Stormpath Default groupId : com . stormpath . spring artifactId : stormpath - default - spring - boot - starter id : stormpath - default - spring - boot - starter description : Spring Boot Default Starter for Stormpath with WebMVC , Thymeleaf and Spring Security version : 1.0.RC9.2 . . .

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

mvn clean install 1 2 mvn clean install

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

brew tap pivotal/tap brew install springboot 1 2 3 brew tap pivotal / tap brew install springboot

You can also install the tools with:

curl start.spring.io/install.sh | bash 1 2 curl start . spring . io / install . sh | bash

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

spring run app.groovy 1 2 spring run app . groovy

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:

Choose: File -> New -> Project… from the menu. Select Spring Initializr . This time, we’ll change the Initializr Service URL to: http://localhost:8080 . Click the Next button. Customize your project and click the Next button. 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 . Click the Next Button. 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.

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!

spring jar start.jar app.groovy 1 2 spring jar start . jar app . groovy

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

➥ java -jar start.jar . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.3.6.RELEASE) ... 1 2 3 4 5 6 7 8 9 10 11 ➥ java - jar start . jar . ____ _ __ _ _ / \ \ / ___ '_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | ' _ | '_| | ' _ \ / _ ` | \ \ \ \ \ \ / ___ ) | | _ ) | | | | | | | ( _ | | ) ) ) ) ' | ____ | . __ | _ | | _ | _ | | _ \ __ , | / / / / ========= | _ | ============== | ___ /=/ _ / _ / _ / : : Spring Boot : : ( v1 . 3.6.RELEASE ) . . .

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!