Spring Boot + Maven Example

Hello friends, In this tutorial, I am going to present a neat and clean explanation to create a Spring Boot hello world application using Spring Tool Suite(STS) and Maven. I hope you read this tutorial carefully, as this is the first spring boot application. It is very simple and understandable.


 

  • 1. Open STS (Spring Tool Suite) -> File -> New -> Maven Project




  •  
  •  

framework

 

  • 2. Put a Tick mark ‘Create a simple project (skip archetype selection)‘ check box > click Next




  •  
  •  

framework
 

  • 3. Give Group Id (package name), Artifact Id (project name) and click Finish




  •  
  •  

framework
 

  • 4. Now you will see a Maven project like,




  •  
  •  

framework


Maven project is created with default setup, then now we will add Spring Boot related stuff in the pom.xml , open pom.xml
By default pom.xml contains


 

< 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.java8s < /groupId>

    <
    artifactId > SpringBootApplications < /artifactId>

    <
    version > 0.0 .1 - SNAPSHOT < /version>

    <
    /project>





Lets add Spring Boot related stuff in it



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>com.java8s</groupId>

<artifactId>SpringBootApplications</artifactId>

<version>0.0.1-SNAPSHOT</version>

<parent>

  <groupId>org.springframework.boot</groupId>

  <artifactId>spring-boot-starter-parent</artifactId>

  <version>1.5.6.RELEASE</version>

  </parent>


<dependencies>

<dependency>

  <groupId>org.springframework.boot</groupId>

  <artifactId>spring-boot-starter-web</artifactId>

</dependency>

</dependencies>

<properties>

<java.version>1.8</java.version>

</properties>

</project>




Explanation



• I have added spring-boot-starter-parent, spring-boot-starter-web and I want to show Spring Boot tutorials in Java 8, so I have added java version.
All Spring Boot projects typically use spring-boot-starter-parent as the parent in pom.xml.

 


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

 



Parent Poms allow you to manage the following things for multiple child projects and modules:

  • Configuration - Java Version and Other Properties

  • Depedency Management - Version of dependencies

  • Default Plugin Configuration





  •  
  •  
  • 6.Now right click on the application -> Maven -> Update Project, If you now observe the directory structure of the project,
  • it will create a new folder named “Maven Dependencies” which contains all supporting .jars to run the Spring Boot application and the Java version also changed to 1.8



    framework



    framework




  •  
  •  
  • 7. Now we will create a java class in src/main/java -> I have created a class named SpringBootExample1.java in com.java8s.app package. I mean the final directory structure looks like…




  •  
  •  

framework



framework
 

SpringBootExample1.java

package com.java8s.app;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

public class SpringBootExample1

{

    public static void main(String[] args) {

        SpringApplication.run(SpringBootExample1.class, args);


    }

}

 

Explanation

Here I have added @SpringBootApplication annotation, that represents the starting point for our Spring Boot application. We have to remember that for every spring boot application we have to create a main class and that need to be annotate with @SpringBootApplication and bootstrap it.
 

  • 8. Finally, right click on the application -> Run As -> Java Application




  •  
  •  

framework
 

  • 9. Select our java class -> ok




  •  
  •  

framework
 

  • 10. Open console and check the output





  •  
  •  

framework



framework

Our Services:

Java Training | Python Training | Machine Learning with Python Training | Deep Learning with Python Training | Data Science with Python Training | Projects

SILAN Technology, BBSR
Call to: 0674-2361252

Comment