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
2. Tick ‘Create a simple project (skip archetype selection)‘ check box > click Next
3. Provide Group Id (its your package), Artifact Id (project name) and click Finish
4. Now you will see a Maven project in your work space, something like..
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
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
10. Open console and check the 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 .:: | ||
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.
Mohan, I have provided a link to download the application, try importing into your STS and see.
Awesome ! Thanks a lot !!
Superrrrrrrrrrr explanation friend………….
Simple and clear, thanks for the article
Thanks for clear and to the point article. Was searching for it from long time.
Wow… Simple and great example.
Thanks a lot… [Edited]
As always simple and easy to understand ….thanks a lot Shiva
thanks
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
Sure Raghu, Soon I will post one example on Spring Boot + Gradle.
Easy to understand and helpful, thank you
Best tutorial sir thank for giving such a best information.
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
thanks
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
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
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 .
Thanks so much. I really appreciate it ❤️
Should we use STS only or shall i prefer Eclipse also?
Simple and straight forward tutorial for spring boot. It really helped me to start working on spring boot.
what is spring tool suite STS and how to use it..?
Super explanation via screenshot
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.
Hi Sivateja,
This is very helpful to us. Can you please post the curd operation using spring boot with restful webservices.
What is bootstrapping and why need to do ? can u explain ??