Spring Boot Project using Maven

In my previous articles of Spring Boot,Β I have covered the introduction of Spring Boot , Its advantages, limitations, goals, features etc and the Key Components of the Spring Boot.

If you have not read my introduction article of Spring Boot, I would request you to take a look – Β Spring Boot Tutorial.

Now we will cover here, How to install and configure Spring Boot in eclipse IDE and develop the Spring Boot Maven project.

There exists following ways to create Spring Boot project. We can use any of below mentioned approach to create Spring Boot Application :

We are going to cover the first way i.e. “Spring Boot Project with Maven” in this article. In our next articles, we will cover other three ways to create Spring Boot Application.

Spring Boot Project with Maven

We are using Eclipse IDE for the below example. It is very popular IDE for java projects. You can download it from here.

Let’s get started :

Step 1 : Create a new Maven ProjectΒ 

Go to File >> New >> Project :

Spring Boot Project with Maven

SelectΒ Maven ProjectΒ from the project wizard and press Next :

Spring Boot Project with Maven

Select the option “create a simple project (skip archetype selection)” and click Next :

Spring Boot Project with Maven

Provide Project name and click on Finish :

Spring Boot Project with Maven

Now our Maven Project is created with the following pom.xml :

Spring Boot Project with Maven

pom.xml :

<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>spring</groupId>
	<artifactId>examples</artifactId>
	<version>0.0.1-SNAPSHOT</version>

</project>

Step 2 : Configure Spring Boot in Maven Project / Add the required dependencies

Firstly add theΒ spring-boot-starter-parentΒ as parent of our Maven project :

<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.1.3.RELEASE</version>
</parent>

The benefit of adding above parentΒ spring-boot-starter-parentΒ is that managing of version becomes easy.

Now , add theΒ spring-boot-starter-webΒ dependency in pom.xml :

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

Now, Add the Java version of the Project in your pom.xml :

<properties>Β Β 
Β Β Β Β <java.version>1.8</java.version>Β Β 
</properties>

Now , Our pom.xml looks like below :

Spring Boot Project with Maven

You can see, Maven has downloaded the jar files :

Spring Boot Project with Maven

Step 3 : Create the main class of Spring Boot

Now, create class inside the src/main/java package in your project :

Spring Boot Project with Maven

Now,Β  Add theΒ @SpringBootApplication annotation in this class andΒ call the static method run of SpringApplication class with the class name as parameter :

Spring Boot Project with Maven

  • @SpringBootApplication indicates this class is a configuration class.
  • SpringApplicationΒ is used here as it bootstraps and auto-configures our application and also it starts the embedded Tomcat server. As we have passed the class name in run method argument, It indicates that this is primary Spring Component for our Project.

Step 4 : Create the controller for Hello World Example

Now, create class inside the src/main/java package in your project :

Spring Boot Project with Maven

Now,

  • Add theΒ @ControllerΒ annotation class level.
  • Create a method that returns String .
  • AddΒ @RequestMapping(“/”)Β andΒ @ResponseBodyΒ  annotation at this method.

Spring Boot Project with Maven

  • @ControllerΒ indicates that class is a Controller.
  • @RequestMappingΒ Β annotation maps HTTP requests to handler methods of controllers.
  • @ResponseBodyΒ is annotation which binds the method return value to the web response body.

Step 5 : Run the Application

Now we have completed all tasks so it’s time to test our application by running it :

Spring Boot Project with Maven

Output :Β 

Spring Boot Project with Maven

Conclusion

That’s all folks! In this article,Β  we have learnt How to develop the Spring Boot Application with Maven.

Newsletter Updates

Enter your name and email address below to subscribe to our newsletter

Leave a Reply