Introduction to Spring Web Application in No Time

Geoffroy Warin

September 2015

 Many official Spring tutorials have both a Gradle build and a Maven build, so you will find examples easily if you decide to stick with Maven. Spring 4 is fully compatible with Java 8, so it would be a shame not to take advantage of lambdas to simplify our code base.

In this article by Geoffroy Warin, author of the book Mastering Spring MVC 4, we will see some Git commands. It's a good idea to keep track of your progress and commit when you are in a stable state.

(For more resources related to this topic, see here.)

Getting started with Spring Tool Suite

One of the best ways to get started with Spring and discover the numerous tutorials and starter projects that the Spring community offers is to download Spring Tool Suite (STS). STS is a custom version of eclipse designed to work with various Spring projects, as well as Groovy and Gradle. Even if, like me, you have another IDE that you would rather work with, we recommend that you give STS a shot because it gives you the opportunity to explore Spring's vast ecosystem in a matter of minutes with the "Getting Started" projects.

So, let's visit https://Spring.io/tools/sts/all and download the latest release of STS. Before we generate our first Spring Boot project we will need to install the Gradle support for STS. You can find a Manage IDE Extensions button on the dashboard. You will then need to download the Gradle Support software in the Language and framework tooling section.

Its recommend installing the Groovy Eclipse plugin along with the Groovy 2.4 compiler, as shown in the following screenshot. These will be needed later in this article when we set up acceptance tests with geb:

We now have two main options to get started.

The first option is to navigate to File | New | Spring Starter Project, as shown in the following screenshot. This will give you the same options as http://start.Spring.io, embedded in your IDE:

The second way is to navigate to File | New | Import Getting Started Content. This will give you access to all the tutorials available on Spring.io. You will have the choice of working with either Gradle or Maven, as shown in the following screenshot:

You can also check out the starter code to follow along with the tutorial, or get the complete code directly.

There is a lot of very interesting content available in the Getting Started Content. It will demonstrate the integration of Spring with various technologies that you might be interested in.

For the moment, we will generate a web project as shown in the preceding image. It will be a Gradle application, producing a JAR file and using Java 8.

Here is the configuration we want to use:

Property

Value

Name

masterSpringMvc

Type

Gradle project

Packaging

Jar

Java version

1.8

Language

Java

Group

masterSpringMvc

Artifact

masterSpringMvc

Version

0.0.1-SNAPSHOT

Description

Be creative!

Package

masterSpringMvc

On the second screen you will be asked for the Spring Boot version you want to use and the the dependencies that should be added to the project.

At the time of writing this, the latest version of Spring boot was 1.2.5. Ensure that you always check out the latest release.

The latest snapshot version of Spring boot will also be available by the time you read this. If Spring boot 1.3 isn't released by then, you can probably give it a shot. One of its big features is the awesome devs tools. Refer to https://spring.io/blog/2015/06/17/devtools-in-spring-boot-1-3 for more details.

At the bottom the configuration window you will see a number of checkboxes representing the various boot starter libraries. These are dependencies that can be appended to your build file. They provide autoconfigurations for various Spring projects.

We are only interested in Spring MVC for the moment, so we will check only the Web checkbox.

A JAR for a web application? Some of you might find it odd to package your web application as a JAR file. While it is still possible to use WAR files for packaging, it is not always the recommended practice. By default, Spring boot will create a fat JAR, which will include all the application's dependencies and provide a convenient way to start a web server using Java -jar.

Our application will be packaged as a JAR file. If you want to create a war file, refer to http://spring.io/guides/gs/convert-jar-to-war/.

Have you clicked on Finish yet? If you have, you should get the following project structure:

We can see our main class MasterSpringMvcApplication and its test suite MasterSpringMvcApplicationTests. There are also two empty folders, static and templates, where we will put our static web assets (images, styles, and so on) and obviously our templates (jsp, freemarker, Thymeleaf). The last file is an empty application.properties file, which is the default Spring boot configuration file. It's a very handy file and we'll see how Spring boot uses it throughout this article.

The last is build.gradle file, the build file that we will detail in a moment.

If you feel ready to go, run the main method of the application. This will launch a web server for us.

To do this, go to the main method of the application and navigate to Run as | Spring Application in the toolbar either by right-clicking on the class or clicking on the green play button in the toolbar.

Doing so and navigating to http://localhost:8080 will produce an error. Don't worry, and read on.

Now we will show you how to generate the same project without STS, and we will come back to all these files.

Getting started with IntelliJ

IntelliJ IDEA is a very popular tool among Java developers. For the past few years I've been very pleased to pay Jetbrains a yearly fee for this awesome editor.

IntelliJ also has a way of creating Spring boot projects very quickly.

Go to the new project menu and select the Spring Initializr project type:

This will give us exactly the same options as STS.

You will need to import the Gradle project into IntelliJ. we recommend generating the Gradle wrapper first (refer to the following Gradle build section).

If needed, you can reimport the project by opening its build.gradle file again.

Getting started with start.Spring.io

Go to http://start.Spring.io to get started with start.Spring.io. The system behind this remarkable Bootstrap-like website should be familiar to you! You will see the following screenshot when you go to the previously mentioned link:

Indeed, the same options available with STS can be found here. Clicking on Generate Project will download a ZIP file containing our starter project.

Getting started with the command line

For those of you who are addicted to the console, it is possible to curl http://start.Spring.io. Doing so will display instructions on how to structure your curl request.

For instance, to generate the same project as earlier, you can issue the following command:

$ curl http://start.Spring.io/starter.tgz \
-d name=masterSpringMvc \
-d dependencies=web \
-d language=java \
-d JavaVersion=1.8 \
-d type=gradle-project \
-d packageName=masterSpringMvc \
-d packaging=jar \
-d baseDir=app | tar -xzvf -
% Total   % Received % Xferd Average Speed   Time   Time     Time Current
Dload Upload   Total   Spent   Left Speed
100 1255 100 1119 100   136   1014   123 0:00:01 0:00:01 --:--:-- 1015
x app/
x app/src/
x app/src/main/
x app/src/main/Java/
x app/src/main/Java/com/
x app/src/main/Java/com/geowarin/
x app/src/main/resources/
x app/src/main/resources/static/
x app/src/main/resources/templates/
x app/src/test/
x app/src/test/Java/
x app/src/test/Java/com/
x app/src/test/Java/com/geowarin/
x app/build.Gradle
x app/src/main/Java/com/geowarin/AppApplication.Java
x app/src/main/resources/application.properties
x app/src/test/Java/com/geowarin/AppApplicationTests.Java

And viola! You are now ready to get started with Spring without leaving the console, a dream come true.

You might consider creating an alias with the previous command, it will help you prototype the Spring application very quickly.

Summary

In this article, we leveraged Spring Boot's autoconfiguration capabilities to build an application with zero boilerplate or configuration files.

We configured Spring Boot tool suite, IntelliJ,and start.spring.io and how to configure it!

Resources for Article:


Further resources on this subject:


You've been reading an excerpt of:

Mastering Spring MVC 4

Explore Title