In this article, we will show you how to develop a Spring Boot REST-style web application.
Technologies used:
- Spring Boot 2.5.5
- Maven 3.x
- Java 8 or later
Note
Spring Boot 2.5.5 requires Java 8.
1. Project directory
Below is a standard Maven folder Structure.

2. Project dependencies
To get started with Spring Boot, we need spring-boot-starter-parent
and spring-boot-starter-web
for the basic REST-style web application.
pom.xml
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.5</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
Below is a complete pom.xml
.
<?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>spring-boot-hello</artifactId> <packaging>jar</packaging> <name>Spring Boot Hello World Example</name> <version>1.0</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.5</version> </parent> <properties> <java.version>17</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-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
3. spring-boot-starter-web
The spring-boot-starter-web
bring in the following main dependencies:
- Spring boot configuration stuff.
- Spring webmvc components.
- Embedded Tomcat 9.0.53.
- Slf4j logback for logging.
- Snakeyaml for external YAML properties.
- Jackson 2.x for JSON binding.
Review the complete spring-boot-starter-web
dependencies in tree structure.
$ mvn dependency:tree
[INFO] Scanning for projects...
[INFO]
[INFO] -------------< org.springframework.boot:spring-boot-hello >-------------
[INFO] Building Spring Boot Hello World Example 1.0
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:3.1.2:tree (default-cli) @ spring-boot-hello ---
[INFO] org.springframework.boot:spring-boot-hello:jar:1.0
[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:2.5.5:compile
[INFO] | +- org.springframework.boot:spring-boot-starter:jar:2.5.5:compile
[INFO] | | +- org.springframework.boot:spring-boot:jar:2.5.5:compile
[INFO] | | +- org.springframework.boot:spring-boot-autoconfigure:jar:2.5.5:compile
[INFO] | | +- org.springframework.boot:spring-boot-starter-logging:jar:2.5.5:compile
[INFO] | | | +- ch.qos.logback:logback-classic:jar:1.2.6:compile
[INFO] | | | | \- ch.qos.logback:logback-core:jar:1.2.6:compile
[INFO] | | | +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.14.1:compile
[INFO] | | | | \- org.apache.logging.log4j:log4j-api:jar:2.14.1:compile
[INFO] | | | \- org.slf4j:jul-to-slf4j:jar:1.7.32:compile
[INFO] | | +- jakarta.annotation:jakarta.annotation-api:jar:1.3.5:compile
[INFO] | | \- org.yaml:snakeyaml:jar:1.28:compile
[INFO] | +- org.springframework.boot:spring-boot-starter-json:jar:2.5.5:compile
[INFO] | | +- com.fasterxml.jackson.core:jackson-databind:jar:2.12.5:compile
[INFO] | | | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.12.5:compile
[INFO] | | | \- com.fasterxml.jackson.core:jackson-core:jar:2.12.5:compile
[INFO] | | +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.12.5:compile
[INFO] | | +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.12.5:compile
[INFO] | | \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.12.5:compile
[INFO] | +- org.springframework.boot:spring-boot-starter-tomcat:jar:2.5.5:compile
[INFO] | | +- org.apache.tomcat.embed:tomcat-embed-core:jar:9.0.53:compile
[INFO] | | +- org.apache.tomcat.embed:tomcat-embed-el:jar:9.0.53:compile
[INFO] | | \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:9.0.53:compile
[INFO] | +- org.springframework:spring-web:jar:5.3.10:compile
[INFO] | | \- org.springframework:spring-beans:jar:5.3.10:compile
[INFO] | \- org.springframework:spring-webmvc:jar:5.3.10:compile
[INFO] | +- org.springframework:spring-aop:jar:5.3.10:compile
[INFO] | +- org.springframework:spring-context:jar:5.3.10:compile
[INFO] | \- org.springframework:spring-expression:jar:5.3.10:compile
[INFO] \- org.springframework.boot:spring-boot-starter-test:jar:2.5.5:test
[INFO] +- org.springframework.boot:spring-boot-test:jar:2.5.5:test
[INFO] +- org.springframework.boot:spring-boot-test-autoconfigure:jar:2.5.5:test
[INFO] +- com.jayway.jsonpath:json-path:jar:2.5.0:test
[INFO] | +- net.minidev:json-smart:jar:2.4.7:test
[INFO] | | \- net.minidev:accessors-smart:jar:2.4.7:test
[INFO] | | \- org.ow2.asm:asm:jar:9.1:test
[INFO] | \- org.slf4j:slf4j-api:jar:1.7.32:compile
[INFO] +- jakarta.xml.bind:jakarta.xml.bind-api:jar:2.3.3:test
[INFO] | \- jakarta.activation:jakarta.activation-api:jar:1.2.2:test
[INFO] +- org.assertj:assertj-core:jar:3.19.0:test
[INFO] +- org.hamcrest:hamcrest:jar:2.2:test
[INFO] +- org.junit.jupiter:junit-jupiter:jar:5.7.2:test
[INFO] | +- org.junit.jupiter:junit-jupiter-api:jar:5.7.2:test
[INFO] | | +- org.apiguardian:apiguardian-api:jar:1.1.0:test
[INFO] | | +- org.opentest4j:opentest4j:jar:1.2.0:test
[INFO] | | \- org.junit.platform:junit-platform-commons:jar:1.7.2:test
[INFO] | +- org.junit.jupiter:junit-jupiter-params:jar:5.7.2:test
[INFO] | \- org.junit.jupiter:junit-jupiter-engine:jar:5.7.2:test
[INFO] | \- org.junit.platform:junit-platform-engine:jar:1.7.2:test
[INFO] +- org.mockito:mockito-core:jar:3.9.0:test
[INFO] | +- net.bytebuddy:byte-buddy:jar:1.10.22:test
[INFO] | +- net.bytebuddy:byte-buddy-agent:jar:1.10.22:test
[INFO] | \- org.objenesis:objenesis:jar:3.2:test
[INFO] +- org.mockito:mockito-junit-jupiter:jar:3.9.0:test
[INFO] +- org.skyscreamer:jsonassert:jar:1.5.0:test
[INFO] | \- com.vaadin.external.google:android-json:jar:0.0.20131108.vaadin1:test
[INFO] +- org.springframework:spring-core:jar:5.3.10:compile
[INFO] | \- org.springframework:spring-jcl:jar:5.3.10:compile
[INFO] +- org.springframework:spring-test:jar:5.3.10:test
[INFO] \- org.xmlunit:xmlunit-core:jar:2.8.2:test
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.147 s
[INFO] Finished at: 2021-10-13T16:16:24+08:00
[INFO] ------------------------------------------------------------------------
4. Developing with Spring Boot
We create a REST controller to return for the root path /
and a @SpringBootApplication
class to start the entire Spring Boot web application.
4.1 REST controller
A simple REST controller returns a message for the root path /
.
HelloController.java
package com.favtuts; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @RequestMapping("/") String hello() { return "Hello World, Spring Boot!"; } }
4.2 @SpringBootApplication
Create a @SpringBootApplication
annotated class, which is the main entry point for the Spring boot application. Run this class, and it will start the entire Spring Boot web application.
MyWebApplication.java
package com.favtuts; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class MyWebApplication { public static void main(String[] args) { SpringApplication.run(MyWebApplication.class, args); } }
5. Spring Boot Test
Below unit test will inject a TestRestTemplate
to test the root path /
to ensure the correct String
is the return.
HelloControllerTest.java
package com.favtuts; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.http.ResponseEntity; import static org.assertj.core.api.AssertionsForClassTypes.assertThat; @SpringBootTest(classes = MyWebApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT) //@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) public class HelloControllerTest { @Autowired private TestRestTemplate template; @Test public void hello_ok() throws Exception { ResponseEntity<String> response = template.getForEntity("/", String.class); assertThat(response.getBody()).isEqualTo("Hello World, Spring Boot!"); } }
6. Demo
On IDE, we run the @SpringBootApplication
annotated class MyWebApplication
directly to start the entire Spring Boot application.
Alternatively, we can use mvn spring-boot:run
to start the Spring Boot application on the command line. By default, Spring Boot starts a self embedded Tomcat at port 8080.
Terminal
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.5.5)
com.favtuts.MyWebApplication : Starting MyWebApplication using Java 11.0.15 on tvt with PID 287472
com.favtuts.MyWebApplication : No active profile set, falling back to default profiles: default
o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
o.apache.catalina.core.StandardService : Starting service [Tomcat]
org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.53]
o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 697 ms
o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
com.favtuts.MyWebApplication : Started MyWebApplication in 1.349 seconds (JVM running for 1.593)
o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
We can use curl
to send an HTTP request to the deployed endpoint.
Terminal
curl localhost:8080
Hello World, Spring Boot!
7. Create an executable JAR
7.1 In pom.xml
, the packaging
to jar
.
pom.xml
<artifactId>spring-boot-hello</artifactId> <packaging>jar</packaging> <name>Spring Boot Hello World Example</name> <version>1.0</version>
And the spring-boot-maven-plugin
is included.
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin>
7.2 A standard mvn package
build will compile and pack everything into an executable JAR file.
$ mvn clean package
$ java -jar target/spring-boot-hello-1.0.jar
We can deploy the executable JAR file target/spring-boot-hello-1.0.jar
into the production server.
8. Download Source Code
$ git clone https://github.com/favtuts/java-spring-boot-tutorials.git
$ cd spring-boot-hello-world
$ mvn spring-boot:run