Get all updates of Java4s
Newsletter

Spring Boot + Maven – Hello World Example Step by Step

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

Lets add Spring Boot related stuff in it

pom.xml

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

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

Comments

3 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. Priyanka says:

    My tomcat server starting but asking for authentication and cab see line tomcat server started on port 8080 on console.

    [main] INFO com.acc.springBootStartapp.CourseAppAPI – Starting CourseAppAPI on HDC2-LX-502308Y with PID 7368 (C:\DeskTop\WorkSpaces\LearningSpringBoot-31Oct17\springBootProject\target\classes started by p.c.khandelwal in C:\DeskTop\WorkSpaces\LearningSpringBoot-31Oct17\springBootProject)
    [main] INFO com.acc.springBootStartapp.CourseAppAPI – No active profile set, falling back to default profiles: default
    [main] INFO org.springframework.context.annotation.AnnotationConfigApplicationContext – Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@3a5ed7a6: startup date [Wed Nov 01 14:09:18 IST 2017]; root of context hierarchy
    [main] INFO org.springframework.jmx.export.annotation.AnnotationMBeanExporter – Registering beans for JMX exposure on startup
    [main] INFO com.acc.springBootStartapp.CourseAppAPI – Started CourseAppAPI in 3.371 seconds (JVM running for 4.326)
    [Thread-1] INFO org.springframework.context.annotation.AnnotationConfigApplicationContext – Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@3a5ed7a6: startup date [Wed Nov 01 14:09:18 IST 2017]; root of context hierarchy
    [Thread-1] INFO org.springframework.jmx.export.annotation.AnnotationMBeanExporter – Unregistering JMX-exposed beans on shutdown

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
What would you like to learn today?
Browse these popular technologies
Spring-Boot-tutorials Hibernate tutorials Spring tutorials Struts tutorials Servlets tutorials Ajax tutorials Log4j tutorials jQuery tutorials Json tutorials Core Java tutorials Web Services tutorials AngularJs tutorials
Most Recent Tutorials
Hibernate_Logo Hibernate Recent Posts Spring_Logo Spring Recent Posts Struts_Logo Struts Recent Posts
Contact | About Us | Privacy Policy | Advertise With Us

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