Spring Boot Project using Maven

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.

Leave a Reply

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