Newsletter

Spring Boot + Maven – Hello World Example Step by Step

Spring-Boot-Tutorials » on Aug 27, 2017 { 26 Comments } By Sivateja

In this article, I am going to explain the steps to create a Spring Boot hello world application using Spring Tool Suite(STS) and Maven. Friends follow this article carefully, as this is the first spring boot application I am going to explain each and every step with screenshot, from the next tutorial on words, I will directly start with directory structure.

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

Spring-Boot-Hello-World-Maven-Project

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

Spring-Boot-Hello-World-Maven-Project-step-1

 

3. Provide Group Id (its your package), Artifact Id (project name) and click Finish
Spring-Boot-Hello-World-Maven-Project-step-2

4. Now you will see a Maven project in your work space, something like..

Spring-Boot-Hello-World-Maven-Directory-Structure

Note: If you observe, its showing ‘JRE System Library [J2SE-1.5]’ as default java version, lets keep it a side for now.

5. So, Maven project is created with default setup, lets add Spring Boot related stuff in the pom.xml ( Guys, hope you have a basic idea about Maven, in pom.xml we will include all dependencies ), 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.java4s</groupId>
  <artifactId>SpringBootHelloWorld</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.java4s</groupId>
  <artifactId>SpringBootHelloWorld</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 at line number 21
  • What is spring-boot-starter-parent? actually this is an existing project given by spring team which contains Spring Boot supporting configuration data (remember just configuration data, it wont download any jars), we have added this in a <parent> tag means, we are instructing Maven to consider our SpringBootHelloWorld project as a child to it, wait for a second, I will show you practically why we have to add spring-boot-starter-parent as parent  🙂
  • In the dependencies, I have added spring-boot-starter-web for web module

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

Spring-Boot-Hello-World-Directory-Structure-After-Maven

Note: Did you observe lines 13-18 of pom.xml? I haven’t included version number for spring-boot-starter-web 🙂 but maven downloaded some jar files with some version(s) related to spring-boot-starter-web, how its possible? that’s because of Maven’s parent child relation. While adding spring boot parent project, I have included version as 1.5.6.RELEASE, so again we no need to add version numbers for the dependencies.  As I told you earlier, spring-boot-starter-parent contains configuration meta data, this means, it knows which version of dependency need to be downloaded.  So we no need to worry about dependencies versions., which will save lot of our time 😉

7.  Now create a java class in src/main/java > I have created one with name SpringBootApp.java in com.java4s.app package. I mean the final directory structure looks like…

 

Note: put your java class in some package, it is mandatory.*  If you haven’t created a package it gives the following error while running your Spring Boot application.

Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package.

SpringBootApp.java

package com.java4s.app;

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

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

Explanation

  • In line number 6, I have added @SpringBootApplication annotation, means this is the starting point for our Spring Boot application
  • In line number 11, I am bootstrapping the application

For now just remember, 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

Spring-Boot-Hello-World-Run-As-Java-Application

9. Select our java class > ok
Spring-Boot-Hello-World-Select-Java-Application

10. Open console and check the output

Spring-Boot-Hello-World-Maven-Output

 

You can check the above output, its saying Started SpringBootApp on 8080, in sometime. That’s it, we have successfully executed a spring boot application 🙂

​​

You Might Also Like

  ::. About the Author .::

Java4s_Author
Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

26 Responses to “Spring Boot + Maven – Hello World Example Step by Step”
  1. Mohan says:

    After following all steps , i am getting the below error.

    Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/core/env/EnvironmentCapable
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$100(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at com.java4s.app.SpringBootApp.main(SpringBootApp.java:10)
    Caused by: java.lang.ClassNotFoundException: org.springframework.core.env.EnvironmentCapable
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    … 13 more

    Appreciate your help to fix the issue.

  2. Sanjay says:

    Awesome ! Thanks a lot !!

  3. Tapas Kumar Badatya says:

    Superrrrrrrrrrr explanation friend………….

  4. Giby alex says:

    Simple and clear, thanks for the article

  5. Sushank says:

    Thanks for clear and to the point article. Was searching for it from long time.

  6. Garun Mishra says:

    Wow… Simple and great example.

    Thanks a lot… [Edited]

  7. pooja says:

    As always simple and easy to understand ….thanks a lot Shiva

  8. arun singh says:

    thanks

  9. Raghu says:

    Hi Sivateja, If possible can you please write/provide the(how to create and run) "Springboot + Gradle" project for us. that should be very helpful for all

  10. Gayathri says:

    Easy to understand and helpful, thank you

  11. Ganapathi Vangapandu says:

    Best tutorial sir thank for giving such a best information.

  12. Dhanaraja says:

    Am getting this while trying to run tomcat in browser as http://localhost:8080/

    Whitelabel Error Page
    This application has no explicit mapping for /error, so you are seeing this as a fallback.

    Tue Sep 18 09:56:25 IST 2018
    There was an unexpected error (type=Not Found, status=404).
    No message available

  13. Dhanaraja says:

    thanks

  14. Jogi says:

    Hi,

    After following all mentioned steps in this article when i am running the application and getting following exception.

    SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
    SLF4J: Defaulting to no-operation (NOP) logger implementation
    SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
    Exception in thread "main" java.lang.NoClassDefFoundError: ch/qos/logback/classic/turbo/TurboFilter
    at java.lang.Class.getDeclaredConstructors0(Native Method)
    at java.lang.Class.privateGetDeclaredConstructors(Unknown Source)
    at java.lang.Class.getConstructor0(Unknown Source)
    at java.lang.Class.getConstructor(Unknown Source)
    at org.springframework.boot.logging.LoggingSystem.get(LoggingSystem.java:169)
    at org.springframework.boot.logging.LoggingSystem.get(LoggingSystem.java:160)
    at org.springframework.boot.logging.LoggingApplicationListener.onApplicationStartingEvent(LoggingApplicationListener.java:229)
    at org.springframework.boot.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:209)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:167)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:122)
    at org.springframework.boot.context.event.EventPublishingRunListener.starting(EventPublishingRunListener.java:69)
    at org.springframework.boot.SpringApplicationRunListeners.starting(SpringApplicationRunListeners.java:48)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:292)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)
    at com.jogi.spring.SpringBootApp.main(SpringBootApp.java:10)
    Caused by: java.lang.ClassNotFoundException: ch.qos.logback.classic.turbo.TurboFilter
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    … 17 more

  15. Ritesh Dalvi says:

    Getting below exception in pom.xml

    Project build error: Non-resolvable parent POM for com.java4s:SpringBootHelloWorld:0.0.1-SNAPSHOT: Failure to transfer org.springframework.boot:spring-boot-starter-parent:pom:1.5.6.RELEASE from https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced. Original error: Could not transfer artifact org.springframework.boot:spring-boot-starter-parent:pom:1.5.6.RELEASE from/to central (https://repo.maven.apache.org/maven2): repo.maven.apache.org and 'parent.relativePath' points at wrong local POM

  16. sivakumar says:

    Hi sivateja,

    I am following your site java4s its good and i am learning from java4s bro if possible make a session for spring boot with micro service .

  17. Amit says:

    Thanks so much. I really appreciate it ❤️

  18. Gnana Bharathi Vuppalapati says:

    Should we use STS only or shall i prefer Eclipse also?

  19. Nagi Reddy says:

    Simple and straight forward tutorial for spring boot. It really helped me to start working on spring boot.

  20. Shubhashish Das says:

    what is spring tool suite STS and how to use it..?

  21. Manju Murugesan says:

    Super explanation via screenshot

  22. ranjini says:

    I'm using eclipse IDE to develop spring boot application . When I try to run localhost:8080 it throws me whilelabel error page -when I checked the console logs it gives me unable to start the application with below stack trace

    Caused by: java.net.BindException: Address already in use: bind
    at sun.nio.ch.Net.bind0(Native Method) ~[na:1.8.0_181]

    2019-05-18 08:54:54.450 INFO 16740 — [ main] .b.l.ClasspathLoggingApplicationListener : Application failed to start with classpath: [file:/C:/Program%20Files%20(x86)/Java/jdk1.8.0_181/jre/lib/resources.jar, file:/C:/Program%20Files%20(x86)/Java/jdk1.8.0_181/jre/lib/rt.ja

    Can someone help me with this issue.

  23. srinivas says:

    Hi Sivateja,
    This is very helpful to us. Can you please post the curd operation using spring boot with restful webservices.

  24. Mohan Kumar says:

    What is bootstrapping and why need to do ? can u explain ??

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.