Create a Quick Web Application Using Spring Boot

How quickly could you put together a new web application from scratch if your employer requested one? Maybe you would need to lug out your old books and brush up on some web applications fundamentals. Even though you are fully committed to figuring it out, you may be a bit worried about how technology might have changed while you where comfortably working away maintaining your employer’s other applications.

Never fear! The good news is that things have been changing for the better. It has never been quicker or easier to get an app up and running. Thanks to the hundreds of open source frameworks shared by various companies and developers, the heavy lifting has already been done for you.  Your job is simply to wire it up and even that is quick and easy, thanks to Pivotal’s Spring Boot.

In this demo, we are going to create a web application for the fictional family in Netflix’s show Bloodline. The Rayburn family needs a new application for registering hotel guests at their seaside resort, Rayburn House, in the Florida Keys. We are going to use Spring Boot to generate a Maven managed Spring MVC Java application with a AngularJS and Bootstrap front end.  You’ll be calling yourself a “Full Stack” developer in no time! Let’s get started:

Generate a Spring Boot Application Using Spring Initializr.

First, navigate to https://start.spring.io/.  For the purposes of this demo, change the drop down boxes at the top to read “Generate a Maven Project with Java and Spring Boot” as shown in Figure 1. The service will default to the current version.  Just under the “Generate Project” button, choose “Switch to the full version” to see all of your options.

Spring Initializr Settings
Figure 1.

Fill out the project metadata as shown in Figure 1.  In the Dependencies section, search for and select these dependencies:

  • Web – Full Stack web development with Tomcat and Spring MVC
  • JPA – Java persistence API including spring-data-jpa, spring-orm, and hibernate.
  • H2- H2 Database (With embedded support)
  • Actuator – Production ready features to help you monitor and manage your application.

When you are ready, click “Generate Project”. This places a zip file in your downloads folder with the same name as your application name. In this case, it is GuestRegistrationSystem.zip.  Extract this somewhere to make it useful. On a side note, you can also generate a spring boot app using a wizard Spring Tool Suite.

Set Up a Development Environment

Now you need to set up a development environment to build your web application in. For this exercise, the IDE that I will be using is Spring Tool Suite (STS). You can download STS here:  https://spring.io/tools/sts/all .  Download the latest for your operating system. Unpack the compressed file, and run its Application file, STS.exe.  STS is simply the Eclipse IDE with the Spring IDE components already installed.

Inside STS, click File -> Import. In the Import Wizard, choose Maven -> Existing Maven Projects as shown in Figure 2, and click next.

STS Import Wizard Settings
Figure 2.

On the import Maven Projects wizard, choose the directory where you extracted your application. In my case it was my desktop. Under projects, make sure pom.xml is chosen, and click Finish. See Figure 3.

STS Import Wizard Settings
Figure 3.

Voilà! You have created a web application! Its almost like magic!

Spring Boot has provided us with a starter web application whose dependencies are managed by Maven in a pom.xml file. Without Spring Boot, the developer would have to download the dependencies, and manually configure them. Spring Boot already knows what you need.  You can view the third party libraries it’s using under the Maven Dependencies folder. Your package explorer in STS should look like Figure 4:

STS Import Wizard Settings
Figure 4.
Make the Application Do Something

Thanks to Spring Boot, we have an application that can start up and run, but at this point, does nothing.  The Rayburn family is paying for a useful app, so we need to show them what it can do.  Let’s start by creating a hello world method that will display a greeting in the browser.

Find and open the GuestRegistrationSystemApplication.java class. It is located in the src/main/java folder and in the com.rayburn.house package.  Make the following edits to the class. Additions are in bold.

package com.rayburn.house;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class GuestRegistrationSystemApplication {

	public static void main(String[] args) {
		SpringApplication.run(GuestRegistrationSystemApplication.class, args);
	}
	
	@RequestMapping("/greetings")
	public String helloWorld() {
		return "Greetings Rayburn Family!";
	}
}
Figure 5.

 

If you are not familiar with Spring MVC annotations, @RequestMapping tells Spring that any time an HTTP request comes in with the path “/greetings”, the user should be routed to the above helloWorld() method.
@RestController tells Spring that this class will not only be a controller, but it will also be a restful web application.
The main method in this class is the main method for the whole application. The SpringApplication.run( ..) method is what starts it all.

Speaking of starting, start your webapp by right clicking on the project GuestRegistrationSystem -> Run As -> Spring Boot App in STS.  In the console window, you will see some activity as Spring Boot starts your app.

STS Import Wizard Settings
Figure 5.

 

Now let’s test the application in a browser. In your favorite browser, navigate to localhost:8080/greetings. Port 8080 is where the GuestRegistrationSystem is running. The greetings path will run your helloWorld() method, thanks to the @RequestMapping annotation. The greeting should appear as below in Figure 6.

Greetings Rayburn Family
Figure 6.

Congratulations! You’ve built a web application from scratch quickly using Spring Boot! However, your clients are going to want an app that does more than greet them. Next we will add a REST layer so that your app will be able to perform more complex tasks.

→ Continue: Add A REST Layer

1 thought on “Create a Quick Web Application Using Spring Boot”

  1. Let me be the first to comment on my own content. One criticism you will hear pertaining to the use of Spring Boot is that it is used as a “crutch” by new developers who haven’t taken the time to learn the Spring framework. But, the fact that Spring Boot exists is evidence of the Spring Framework being overly complex. It has a reputation for having a steep learning curve. Let Spring Boot be your starting off point if you aren’t familiar with the framework. Then, dig in and learn.

Leave a Reply

Your email address will not be published. Required fields are marked *