Spring Boot Maven Application

In the previous article, you have learned about Spring Boot System Requirements. In this article, you will learn about steps to create Spring Boot Application in the Maven environment.

  1. Check Java Version
  2. Check Maven Version
  3. Create Application in Maven
  4. Add Spring Boot Parent
  5. Add spring-boot-stater-
  6. Add Business Functionality
  7. Build & Run

Here you will know in detail about each step.

Check Java Version

You can run command java -version to get the current java version installed in your machine.

Java Version

If you are not getting version as above in screen then follow steps in this link: Setup Java/JDK and Eclipse On Window & Linux

Check Maven Version

You can run command mvn -version to get the current Maven version installed in your machine.

Maven Version

If you are not getting version as above in screen then follow steps in this link: Configure Maven in Window and Linux?

Create Application in Maven

To Create a Maven-based Web application, go to the directory where you want to create an application run below maven command.


mvn archetype:generate -DgroupId=com.fiot  -DartifactId=SpringBootMavenApp -DarchetypeArtifactId=maven-archetype-quickstart  -DinteractiveMode=false

Create Maven Application

It will create a java maven application with the below directory structure.


SpringBootMavenApp
-src
--main
---java
----com
-----fiot
------App.java
--test
---java
----com
-----fiot
------AppTest.java
-pom.xml

Add spring-boot-stater

Added highlighted lines of the statement in pom.xml to make your application Spring Boot Web Application.

Spring Boot Maven POM

Let’s discuss in detail these three sections.

spring-boot-starter-parent: It’s a special starter that inherits useful maven defaults. It also provides dependency management so that omit version tags for dependencies.

spring-boot-starter-web: It’s add in the dependencies section to make application as a web which will add all required dependencies for a web application.

Also See: Spring Boot Starters List

spring-boot-maven-plugin: This plugin is used to create an executable jar of your application.

Add Business Functionality

Now your application is having Spring Boot features. You can add your business functionality as per requirement. In this below code we can create one REST service which returns the response as “Hello World!”.

package com.fiot;

import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;

@RestController
@EnableAutoConfiguration
public class App
{
	@RequestMapping("/")
    String home() {
        return "Hello World!";
    }

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

}

Here this main method is the starting point of your Spring Boot application that will start Spring container, create beans, load configuration and perform required actions to start the application.

@RestController: This is stereotype annotation which helps to understand the users and Spring framework like whats the purpose of this class like Spring will consider this class when any web request will come.

@RequestMapping: This annotation provides routing information. In this example, tell Spring that any HTTP request with the path “/” should be mapped to the home method.

@EnableAutoConfiguration: This is class-level annotation, which tells to spring boot to guess how you want to configure spring based on configured starters in classpaths and jars. Since in pom.xml we have added spring-boot-starter-web added Tomcat and Spring MVC, the auto-configuration will assume you want to develop a web-based application.

Build & Run

To build your application just go to the root directory of the application and run below command.


mvn package

When you run this command it will take time because download required dependencies from maven repository and after successful compile and package. It will create one more folder as a target in the root of your application.

To run your application use below command.


java -jar ./target/SpringBootMavenApp-1.0-SNAPSHOT.jar

When an application successfully executed then got to the browser and in address bar use this URL http://localhost:8080 after submit you will get a response as “Hello World!!”.

One thought on “Spring Boot Maven Application”