Build a Spring Boot Application in Under 5 Minutes

Prerequisites
  • Have Eclipse Installed
  • Basic Understanding of Java, how classes work.
  • Understand what an HTTP request and response is.

Let's install Spring Tool Suite. Select help -> Install New Software. Select the latest version of Spring Tool Suite and proceed to install it.

Next create a new spring boot project. Select file -> new -> project. Enter the information shown in these screenshots.

When you get to this page, select Spring Web to include the Spring Web dependency in your pom.xml. This dependency contains the necessary code to make receiving requests from users and sending responses as easy as possible. Much of the lower-level code has been written for you, and you simply need to specify the url paths to look for, and the responses to send (ok, it can get a bit more complicated, but for this tutorial, it will be almost that easy!).

Let's take a look at SpringBootHelloWorldApplication.java which was generated automatically.


package com.SpringBootHelloWorld.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootHelloWorldApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringBootHelloWorldApplication.class, args);
	}

}


				

If you wanted to, you could add code in the main method that would be executed when the application starts. For example, you could check environment variables and print them here.

Create a new class as shown below

As shown in code below, add the @Controller annotation, and the below method. The controller annotation tells springboot that this class will be looking for URL patterns and produce output to requests to those URL patterns.

The @RequestMapping annotation tells SpringBoot a specific URL pattern to look for. And the @ResponseBody tells SpringBoot to return some literal text, rather than an html page or template. We will learn more about different ways to output responses in subsequent tutorials.


package com.SpringBootHelloWorld.app;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class MainController {

	@RequestMapping("aloha")
	@ResponseBody
	public String aloha() {
		System.out.println("aloha");
		return "aloha";
	}
}

In source/main/resources, you should see a file called application.properties. We're going to add one property to it that will make your development easier. Simply add:

server.port=8077

This is an arbitrary port number that will ensure our app does not use any ports that are already in use.

In Boot Dashoard view, right click your app, and select (Re)start. If you do not see the Boot Dashboard, open it from Window->Show View.

Visit http://localhost:8077/aloha in your browser, and you should see the text printed in your browser.



Next Lesson: Spring Boot MVC Example in Eclipse - Build a Spring Boot MVC Application in Under 10 Minutes

Troubleshooting

Seeing the above? Follow these steps.