Spring Boot hello world example – Thymeleaf

In this post, we will see how to create Spring boot hello world example using Thymeleaf.

Why Spring Boot?

Let’s say you want to create Spring MVC MySQL project.When you are working on this project, you might be confused with a lot of dependencies it requires.You might get confused with versions too.
Spring boot will solve above problems.Spring Boot will help you in creating Spring applications faster. It comes with all required dependencies and embedded server.You don’t have worry about version mismatch or compatibility issues.
Let’s see how Spring boot makes over life simpler.I am going to create Spring MVC hello world example using Spring Boot.

Github Source code:

Spring Boot hello world example using Thymeleaf:

Here are steps to create a Spring boot hello word example Using Thymeleaf.

Project structure:

Spring Boot Thymeleaf Project structure

 

Tools used for creating below project:

  1. Spring Boot 1.5.3.RELEASE
  2. Spring 4.3.8.RELEASE
  3. Thymeleaf 2.5.1 RELEASE
  4. Tomcat Embed 8
  5. Maven 3
  6. Java 8
  7. Eclipse
Step 1: Create a dynamic web project using maven in eclipse named “SpringBootHelloWorldExampleThymeleaf”.

Maven dependencies:

Step 2: Change pom.xml as below:

The spring-boot-starter-parent provides you all maven defaults required for any spring project.
Since we are developing a web application using thymeleaf, we also need to add spring-boot-starter-thymeleaf dependency.
This will include additional dependencies such Spring boot, thymeleaf, tomcat etc which are required for this application.
If you notice, we did not provide any version for specific components. You just need to provide version no.(1.5.3.RELEASE) for spring boot.
step 3: Create a package named "org.arpit.java2blog.springboot"
create a controller class named "HelloWorldController.java"

As request first goes to dispatcherServlet and it redirects to controller class. Here @Controller depicts that this is our controller class. @RequestMapper is used to map incoming HTTP request to handler method(hello() in above controller).So hello() method of HelloWorldController.java will handle GET request from dispatcher.So when we have url of

above method will get called.

Step 4: Create a package named “org.arpit.java2blog”
create a class named “SpringBootHelloWorldApplication.java”

We have just added @SpringBootApplication and it does all the work.
Let’s understand more about this annotation.
@SpringBootApplication is a annotation that adds all of the following:

@Configuration makes the class as a source of bean definitions for the application context.
@EnableAutoConfiguration enables Spring boot to add beans presents in classpath setting and various property setting.
Normally you would add @EnableWebMvc for a Spring MVC application, but Spring Boot adds it automatically when it sees spring-webmvc on the classpath.
This flags the application as a web application and activates key behaviors such as setting up a DispatcherServlet.
@ComponentScan tells Spring to look for other components, configurations, and services in the the default package, allowing it to find the controllers.
If specific packages are not defined, scanning will occur from the package of the class that declares this annotation.

Step 5: Create a property file named application.properties as below and put it in src/main/resoures.

Above property is used to disable caching of templates.

Step 6:Create a folder named “templates” in src/java/resources folder and create a file named “index.html” as below:

Step 7: Create hello.html in above created templates folder

That’s all about Spring Boot hello world example using thymeleaf.
Step 8: It ‘s time to do maven build.

Right click on project -> Run as -> Maven build

Step 9: Provide goals as clean install spring-boot:run (given below) and click on run
Spring Boot Thymeleaf maven build
you will see below output at console:

[INFO] Scanning for projects…
[INFO] [INFO] ————————————————————————
[INFO] Building SpringBootHelloWorldExampleThymeleaf Maven Webapp 0.0.1-SNAPSHOT
[INFO] ————————————————————————
[INFO] [INFO] — maven-clean-plugin:2.6.1:clean (default-clean) @ SpringBootHelloWorldExampleThymeleaf —
[INFO] Deleting /Users/apple/Documents/WorkspaceAlgo/SpringBootHelloWorldExampleThymeleaf/target
[INFO] [INFO] — maven-resources-plugin:2.6:resources (default-resources) @ SpringBootHelloWorldExampleThymeleaf —
[INFO] Using ‘UTF-8’ encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 2 resources
[INFO] [INFO] — maven-compiler-plugin:3.1:compile (default-compile) @ SpringBootHelloWorldExampleThymeleaf —
[INFO] Changes detected – recompiling the module!
[INFO] Compiling 2 source files to /Users/apple/Documents/WorkspaceAlgo/SpringBootHelloWorldExampleThymeleaf/target/classes
[INFO] [INFO] — maven-resources-plugin:2.6:testResources (default-testResources) @ SpringBootHelloWorldExampleThymeleaf —
[INFO] Not copying test resources
[INFO] [INFO] — maven-compiler-plugin:3.1:testCompile (default-testCompile) @ SpringBootHelloWorldExampleThymeleaf —
[INFO] Not compiling test sources
[INFO] [INFO] — maven-surefire-plugin:2.18.1:test (default-test) @ SpringBootHelloWorldExampleThymeleaf —
[INFO] Tests are skipped.
[INFO] [INFO] — maven-war-plugin:2.6:war (default-war) @ SpringBootHelloWorldExampleThymeleaf —
[INFO] Packaging webapp
[INFO] Assembling webapp [SpringBootHelloWorldExampleThymeleaf] in [/Users/apple/Documents/WorkspaceAlgo/SpringBootHelloWorldExampleThymeleaf/target/SpringBootHelloWorldExampleThymeleaf] [INFO] Processing war project
[INFO] Copying webapp resources [/Users/apple/Documents/WorkspaceAlgo/SpringBootHelloWorldExampleThymeleaf/src/main/webapp] [INFO] Webapp assembled in [375 msecs] [INFO] Building war: /Users/apple/Documents/WorkspaceAlgo/SpringBootHelloWorldExampleThymeleaf/target/SpringBootHelloWorldExampleThymeleaf.war
[INFO] [INFO] — maven-install-plugin:2.5.2:install (default-install) @ SpringBootHelloWorldExampleThymeleaf —
[INFO] Installing /Users/apple/Documents/WorkspaceAlgo/SpringBootHelloWorldExampleThymeleaf/target/SpringBootHelloWorldExampleThymeleaf.war to /Users/apple/.m2/repository/org/arpit/java2blog/SpringBootHelloWorldExampleThymeleaf/0.0.1-SNAPSHOT/SpringBootHelloWorldExampleThymeleaf-0.0.1-SNAPSHOT.war
[INFO] Installing /Users/apple/Documents/WorkspaceAlgo/SpringBootHelloWorldExampleThymeleaf/pom.xml to /Users/apple/.m2/repository/org/arpit/java2blog/SpringBootHelloWorldExampleThymeleaf/0.0.1-SNAPSHOT/SpringBootHelloWorldExampleThymeleaf-0.0.1-SNAPSHOT.pom
[INFO] [INFO] >>> spring-boot-maven-plugin:1.5.3.RELEASE:run (default-cli) > test-compile @ SpringBootHelloWorldExampleThymeleaf >>>
[INFO] [INFO] — maven-resources-plugin:2.6:resources (default-resources) @ SpringBootHelloWorldExampleThymeleaf —
[INFO] Using ‘UTF-8’ encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 2 resources
[INFO] [INFO] — maven-compiler-plugin:3.1:compile (default-compile) @ SpringBootHelloWorldExampleThymeleaf —
[INFO] Nothing to compile – all classes are up to date
[INFO] [INFO] — maven-resources-plugin:2.6:testResources (default-testResources) @ SpringBootHelloWorldExampleThymeleaf —
[INFO] Not copying test resources
[INFO] [INFO] — maven-compiler-plugin:3.1:testCompile (default-testCompile) @ SpringBootHelloWorldExampleThymeleaf —
[INFO] Not compiling test sources
[INFO] [INFO] <<< spring-boot-maven-plugin:1.5.3.RELEASE:run (default-cli) < test-compile @ SpringBootHelloWorldExampleThymeleaf <<<
[INFO] [INFO] — spring-boot-maven-plugin:1.5.3.RELEASE:run (default-cli) @ SpringBootHelloWorldExampleThymeleaf —. ____ _ __ _ _
/\ / ___’_ __ _ _(_)_ __ __ _
( ( )___ | ‘_ | ‘_| | ‘_ / _` |
\/ ___)| |_)| | | | | || (_| | ) ) ) )
‘ |____| .__|_| |_|_| |___, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.3.RELEASE)2017-04-28 16:40:05.395 INFO 1967 — [ main] o.a.j.SpringBootHelloWorldApplication : Starting SpringBootHelloWorldApplication on apples-MacBook-Air.local with PID 1967 (/Users/apple/Documents/WorkspaceAlgo/SpringBootHelloWorldExampleThymeleaf/target/classes started by apple in /Users/apple/Documents/WorkspaceAlgo/SpringBootHelloWorldExampleThymeleaf)
2017-04-28 16:40:05.403 INFO 1967 — [ main] o.a.j.SpringBootHelloWorldApplication : No active profile set, falling back to default profiles: default
2017-04-28 16:40:06.087 INFO 1967 — [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot[email protected]330dfb29: startup date [Fri Apr 28 16:40:06 IST 2017]; root of context hierarchy
2017-04-28 16:40:07.740 INFO 1967 — [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-04-28 16:40:07.756 INFO 1967 — [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2017-04-28 16:40:07.757 INFO 1967 — [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.14
2017-04-28 16:40:07.926 INFO 1967 — [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2017-04-28 16:40:07.927 INFO 1967 — [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1840 ms
2017-04-28 16:40:08.140 INFO 1967 — [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: ‘dispatcherServlet’ to [/] 2017-04-28 16:40:08.145 INFO 1967 — [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: ‘characterEncodingFilter’ to: [/*] 2017-04-28 16:40:08.145 INFO 1967 — [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: ‘hiddenHttpMethodFilter’ to: [/*] 2017-04-28 16:40:08.146 INFO 1967 — [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: ‘httpPutFormContentFilter’ to: [/*] 2017-04-28 16:40:08.146 INFO 1967 — [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: ‘requestContextFilter’ to: [/*] 2017-04-28 16:40:08.564 INFO 1967 — [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot[email protected]330dfb29: startup date [Fri Apr 28 16:40:06 IST 2017]; root of context hierarchy
2017-04-28 16:40:08.656 INFO 1967 — [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped “{[/]}” onto public java.lang.String org.arpit.java2blog.springboot.HelloWorldController.index()
2017-04-28 16:40:08.658 INFO 1967 — [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped “{[/helloworld]}” onto public org.springframework.web.servlet.ModelAndView org.arpit.java2blog.springboot.HelloWorldController.hello()
2017-04-28 16:40:08.662 INFO 1967 — [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped “{[/error]}” onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2017-04-28 16:40:08.663 INFO 1967 — [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped “{[/error],produces=[text/html]}” onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2017-04-28 16:40:08.707 INFO 1967 — [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2017-04-28 16:40:08.707 INFO 1967 — [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2017-04-28 16:40:08.762 INFO 1967 — [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2017-04-28 16:40:09.402 INFO 1967 — [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-04-28 16:40:09.496 INFO 1967 — [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-04-28 16:40:09.502 INFO 1967 — [ main] o.a.j.SpringBootHelloWorldApplication : Started SpringBootHelloWorldApplication in 4.768 seconds (JVM running for 14.268)

Step 10: Let’s test the application now.
As Spring uses embedded tomcat, you can access the project using http://localhost:8080/.

Spring Boot Thymeleaf Index

When you click on above link, you will get below screen.
Spring Boot Thymeleaf Hello World
That’s all about Spring boot hello world example using Thymeleaf. If you find any issue with this project, please put a comment.


import_contacts

You may also like:

Related Posts

  • 11 January

    Spring boot JDBC example

    Table of ContentsTools usedProject StructureCreate new Spring boot projectMaven configurationConfigure datasource in Application.propertiesCreate database tableCreate model classCreate Repository interface and implementationCreate service interface and implementationCreate Application classRun the applicationVerify database outputGithub Source code In this post, we will see Spring boot JDBC example. As we already know Spring boot does lot of auto configurations which […]

  • 02 January

    Spring boot devtools tutorial

    Table of ContentsOverviewJRebelIntroduction to Spring Boot DevToolsAdding Devtools to your projectSpring Boot DevTools FeaturesProperty DefaultsLoggingCachingAutomatic RestartLive ReloadGlobal SettingsRemote ApplicationsFile System WatchersConclusion In this post, we’ll be looking up what does spring has to offer for developer experience. Overview Using IDEs like Eclipse, Spring Tool Suite, Intellij Idea a typical development setup includes an application code […]

  • 26 December

    Spring boot H2 Database example

    Table of ContentsWhat is H2 database?Tools usedGithub Source code:Project StructureCreate new Spring boot projectMaven configurationH2 database configurationOverride H2 databse defaultsModelRepositoryServiceControllerSpring boot main fileRun the applicationTest the applicationCreate movie objectGet all movie objectsDelete movie id 3 In this post, we are going to see how to create Spring boot application integrating with H2 database. What is […]

  • 22 December

    Spring Boot CrudRepository

    Table of ContentsTools usedProject Structure:Wiring up MongoDBThe Repository ClassThe Save methodThe User Entity ClassBringing the UserDao and CrudRepository togetherServiceControllerApplication classRunning the code sampleConclusionSource code In this post, we’ll be looking up how to create and use Spring Boot CrudRepository. SpringBoot CrudRepository provides sophisticated CRUD functionality for the type of entity you want to be managed.This […]

  • 10 April

    Difference between Spring and Spring boot

    Table of ContentsSpring frameworkSpring bootAdvantages of Spring BootSpring vs Spring boot In this post, we will see difference between Spring and Spring boot. If you have followed spring project over time, you might have noticed it has become increasingly complex. If you want to create a spring application, you still need to put a lot […]

  • 17 September

    Spring Boot SOAP Web service Example

    Table of ContentsTools usedProject structureAdd wsdl4j dependencyXSD file to define domainGenerate domain classes based on XSDCreate Book repository with dummy dataDefine Book service endpointDefine web service configurationRun the applicatonTest WSDL URLTest Spring soap web serviceSource code In this post, we will see how to create soap web services with Spring boot. We will create contract […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.