OctoPerf
ZI Les Paluds, 276 Avenue du Douard, 13400 Aubagne, France
+334 42 84 12 59
[email protected]
Development
2018-02-22
2023-02-13
I’m sure you’re looking for a complete
Spring Rest Tutorial
which covers the most important topics related to
Spring Boot
. You’re in the right place!
You want to build a web application or a
REST API
using Spring Boot (and other popular technologies like Thymeleaf), but you don’t know where to start… Let me help you get things done. This tutorial explains how to create a simple Rest Api
exposing data as Json
.
Don’t worry, Spring isn’t that difficult!
In under 5 minutes
, you will build your first web app using Spring Boot.
NOTE
: Updated with
Spring Boot 2
and
Spring 5
!
Full source code is available at
Spring Boot 2 Demo
on Github.
Spring Initializr
To get started quickly, please generate a
Sample Web
project using
Spring Initializr
. Some of the sections below review part of the code being generated by Spring Initializr.
Even if you could not use Spring Initialzr, you should be able to follow this tutorial.
Maven Module Pom
First, we need to create a Maven Project with the following
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>
<groupId>
com.octoperf
</groupId>
<artifactId>
demo
</artifactId>
<version>
0.0.1-SNAPSHOT
</version>
<packaging>
jar
</packaging>
<name>
demo
</name>
<description>
Demo project for Spring Boot
</description>
<parent>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-parent
</artifactId>
<version>
2.0.1.RELEASE
</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>
UTF-8
</project.build.sourceEncoding>
<project.reporting.outputEncoding>
UTF-8
</project.reporting.outputEncoding>
<java.version>
1.8
</java.version>
</properties>
<dependencies>
<dependency>
<groupId>
org.projectlombok
</groupId>
<artifactId>
lombok
</artifactId>
</dependency>
<dependency>
<groupId>
com.fasterxml.jackson.core
</groupId>
<artifactId>
jackson-core
</artifactId>
</dependency>
<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>
Parent
:
spring-boot-starter-parent
is a convenient Maven Parent POM configured with everything needed to run a
Spring Boot
application. By defining it as a parent, most dependencies like
lombok
or
jackson
are already managed (no need to specify a version),
Dependencies
:
lombok
: provides annotations to generate most of the boiler plate code (like constructors, equals and hashcode methods etc.),
jackson-core
:
Jackson
is a popular Java Json serialization framework,
spring-boot-starter-web
is Spring dependencies which imports everything needed to build a web application using Spring Boot.
By adding Jackson as a dependency, Spring Boot automatically configures The Rest endpoints with
Jackson
serializer.
Please make sure to enable
Annotation Processing
within the IDE. It can be done in
Build > Compiler > Annotation Processors
in Intellij (
Enable Annotation Processing
, and
Obtain processors from project classpath
).
Now, we need to create a main application which will bootstrap Spring Boot.
Spring-Boot Bootstrap
If you’re already so far, you have a working maven project in your favorite IDE (like
Intellij
). It’s time to create the main application:
package
com.octoperf
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
@SpringBootApplication
public
class
DemoApplication
{
public
static
void
main
(
String
[]
args
)
{
SpringApplication
.
run
(
DemoApplication
.
class
,
args
);
I’ve created the class
DemoApplication
in the
com.octoperf.demo
package. When the Java application will be run, the execution will be directly delegated to the
SpringApplication
class.
SpringApplication
bootstraps and launches a Spring application from a Java main
method. By default it will perform the following steps to bootstrap your
application:
Create an appropriate
ApplicationContext
instance (depending on your classpath),
Register a
CommandLinePropertySource
to expose command line arguments as Spring properties,
Refresh the application context, loading all singleton beans,
And Trigger any
CommandLineRunner
beans.
What is an
ApplicationContext
?
Central interface to provide configuration for an application. This is read-only while the application is running, but may be reloaded if the implementation supports this.
What is a
CommandLinePropertySource
?
Abstract base class for {@link PropertySource} implementations backed by command line arguments. The parameterized type
T
represents the underlying source of command line options.
What is a
CommandLineRunner
?
Interface used to indicate that a bean should
run
when it is contained within a
SpringApplication
.
If we take a look at what the
@SpringBootApplication
does:
@Target
(
ElementType
.
TYPE
)
@Retention
(
RetentionPolicy
.
RUNTIME
)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan
(
excludeFilters
=
{
@Filter
(
type
=
FilterType
.
CUSTOM
,
classes
=
TypeExcludeFilter
.
class
),
@Filter
(
type
=
FilterType
.
CUSTOM
,
classes
=
AutoConfigurationExcludeFilter
.
class
)
})
public
@interface
SpringBootApplication
{
It’s an annotation which enables various features like
Spring Boot Auto-Configuration
and
component scanning
.
Spring will automatically discover any Spring annotated class (like
@Component
or
@Service
) under the
com.octoperf.demo
and instantiate them on application startup.
So yeah,
there is a lot of magic
behind the scene, but no worries! You have plenty of time to master Spring internals once you understand the basics.
Person Bean
Let’s now create a
Person
bean within the same package. This bean represents a Person:
package
com.octoperf
;
import
lombok.Data
;
@Data
public
class
Person
{
String
firstname
,
lastname
;
Thanks to lombok’s
@Value
annotation, the bean is pretty simple! Lombok takes care of generating the constructor, equals/hashcode methods, getters and define all fields as
private final
.
We’re going to send this bean over the wire by serializing it into Json.
Spring MVC Controller
Now let’s expose the
Person
bean through a Spring MVC Controller:
package
com.octoperf
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RestController
;
@RestController
@RequestMapping
(
"/person"
)
class
PersonController
{
@GetMapping
(
"/hello"
)
public
Person
hello
()
{
final
Person
person
=
new
Person
();
person
.
setFirstname
(
"John"
);
person
.
setLastname
(
"Smith"
);
return
person
;
This endpoint exposes the path
/person/hello
, which returns a
Person
instance with
John
as firstname, and
Smith
as lastname.
Let’s edit the application.properties to configure the
server.port
property which controls the port on which the Spring Boot server will run: