Spring Boot Maven Example Hello World walks you through the process of creating a example spring boot step by step. Spring Boot is sub-project developed by developers of spring framework, Spring Boot makes it easy to create stand-alone, production-grade Spring based applications with minimum configuration possible.
Other interesting posts you may like
Let’s begin:
Project structure
We will create a project with directory structure like below
Maven Dependencies
To create Spring Boot Maven Example Hello World, you need to add dependencies into pom.xml file like below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
<?xml version="1.0" encoding="UTF-8"?> <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> <artifactId>SpringBootMavenExample</artifactId> <name>SpringBootMavenExample</name> <description>SpringBootMavenExample</description> <packaging>war</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.2.RELEASE</version> </parent> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> |
Let’s dig deeper:
We are developing a web application so we will add a spring-boot-starter-web dependency in pom.xml like below:
1 2 3 4 |
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> |
Our application needs to use the Tomcat libraries at runtime, the we should append the below dependency in pom.xml file and set scope of the dependency to provided.
1 2 3 4 5 |
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> |
In order to enable JSP support using Tomcat we need to add dependencies into pom.xml like below:
1 2 3 4 5 |
<dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> |
In this example, we are using maven plugin so we need to add the below dependency into pom.xml file:
1 2 3 4 |
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> |
Configure Spring Web Application
To set our application up as a servlet application we extend our main class with SpringBootServletInitializer and override the configure method using SpringApplicationBuilder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package com.javabycode.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.context.web.SpringBootServletInitializer; @SpringBootApplication public class MyWebApplication extends SpringBootServletInitializer{ @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(MyWebApplication.class); } public static void main(String[] args) throws Exception { SpringApplication.run(MyWebApplication.class, args); } } |
Configure properties file
1 2 |
spring.mvc.view.prefix: / spring.mvc.view.suffix: .jsp |
Create the Controller
We create a simple controller named HelloWorldController like below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package com.javabycode.springboot; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller public class HelloWorldController { @RequestMapping("/hello") public String hello(Model model, @RequestParam(value="name", required=false, defaultValue="World") String name) { String message="You just create Spring Boot Example successfully"; model.addAttribute("name", name); model.addAttribute("message", message); return "hello"; } } |
If you have any issues with creating Spring controller you can see detail the tutorial Spring MVC Tutorial Step by Step for Beginners.
Create the View
We create a view using jsp page to display “Hello World” words and other message.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Hello</title> </head> <body> Hello ${name} </br> ${message} </body> </html> |
Run Application
To deploy and run our Spring Boot Maven Example Hello World on web container, we run the below command in console
1 |
mvn spring-boot:run |
Note: You must be in your project directory before run that command.
While running that command, you will see the output like below
After running successfully, you can access the application via URL http://localhost:8080 and see the homepage such as
Click the link “Click me to say Hello” then the Hello World page is displayed
That’s it on the tutorial Spring Boot Maven Example Hello World with JSP.
Download complete source code, please click link below
SpringBootMavenExampleHelloWorld.zip (418 downloads)
Source code on Github https://github.com/javabycode/spring-boot-maven-example-helloworld