Home » Spring Boot » Spring Boot Project With Spring Initializr

Spring Boot Project With Spring Initializr

Create Spring Boot Application using Spring Initializr

Spring Boot had been built for Rapid Application Development. In this tutorial we will learn How to Create Spring Boot Application using Spring Initializ(start.spring.io).

The best way of bootstrapping Spring Boot application is by using Spring Initializr.It is a web tool which is provided by Spring on official site. Spring Initializr can generate a Spring Boot project structure for you. It doesn’t generate any application code, but it will give you a basic project structure and either a Maven or a Gradle build specification to build your code with. All you need to do is write the application code.


Using Spring Initializr Web Interface :

Spring Initializr helps us to setup Web applications very easily.The most straightforward way to use the Spring Initializr is to point your web browser to https://start.spring.io. You should see a form similar the below screen.

Spring Boot Initializr


As we are going to use Maven as Build Tool so for now select Maven as build tool. We would be using Spring Boot 1.5.8. In the Group provide your base package name. I have given com.jwt.spring.boot as Group and Artifact as SpringBootFirstApp. Artifact is name of your project.

Next we need to choose the project dependencies. As we are going to develop a web application so we need to select Starter as Web. Screen shot with complete details is given below.

Spring Boot Initializr


After providing all the details click on "Generate Project" button which will generate a Maven Project with the name provided in the Artifact and download it in your local as a zip file.Now what you have to do simply extract the zip file to your desired directory and import the extracted project as Maven Project in Spring Tool Suite(STS) as mentioned below.

Open STS and Click on File -> Import which will open Import Screen. In that screen Expand Maven and select Existing Maven Project and click on Next button as shown below.

Spring Boot Initializr



In the next screen click on Browse button and select the project which you extracted and then click on Finish button.

Spring Boot Initializr



Once import is done you can see your project in the STS.Our Spring Boot Project should look like this.

Spring Boot Initializr

If you observe the project files, it generates pom.xml file, Spring Boot Java file and Test Java file which is given below.

pom.xml

This is A Maven build specification for Spring Boot Web Application.


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.jwt.spring.boot</groupId>
	<artifactId>SpringBootFirstApp</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>SpringBootFirstApp</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.8.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>


</project>
						

SpringBootFirstAppApplication.java

This class is generated by Spring Initializr which contains a main() method to bootstrap the application.


package com.jwt.spring.boot.SpringBootFirstApp;

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

@SpringBootApplication
public class SpringBootFirstAppApplication {

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

						

SpringBootFirstAppApplicationTests.java

This class is generated by Spring Initializr which is an empty JUnit test class instrumented to load a Spring application context using Spring Boot auto-configuration.


package com.jwt.spring.boot.SpringBootFirstApp;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootFirstAppApplicationTests {

	@Test
	public void contextLoads() {
	}

}
						

application.properties

This empty properties file is created by Spring Initializr to add application configuration properties externally.

static and templates directory

The static directory is where you can put any static content (JavaScript, stylesheets, images, etc) to be served from the web application. And, as you’ll see later, you can put templates that render model data in the templates directory.

Create Controller Class

To be able to handle web request, our application should have controllers. Here is a sample controller HelloWorldController that handles the request to the path "/hello". Create a simple rest controller class inside "com.jwt.spring.boot.controller" package as follows.


package com.jwt.spring.boot.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloWorldController {

	@RequestMapping("/hello")
	public String sayHello() {
		return "Hello World Spring Boot!!";
	}

}

						

Final Project Structure

Final Structure of the project is given below. Please review tour project structure before running the app.

Spring Boot Initializr


Running the Application

Finally, we can now run our simple spring boot web application. To run, open the class "MainApp.java" then right click and run or debug to debug the app. This will display the spring boot banner and logs in the console as shown below.

Spring Boot Hello World


Now, enter the http://localhost:8080/hello in browser's address bar and see the output.

Spring Boot Hello World



Download Code

In the next article I will discuss about Spring Boot – How to Change Default Tomcat Server Port.


References:

Spring Boot Official Documentation


Previous Next Article