Spring Boot is a powerful Spring module who allow to use Spring framework in easy and fast way.
In particular, the main advantages are:
- Building apps in simple way because it has already some configurations out of box (like infrastructural beans).
- It integrates Tomcat, then not be necessary to deploy the app in a servlet container or application server
(you can just run the jar for execute the app).
Prerequisites
- Set up a jdk (we will use the version 8, but you can use successive versions).
- Installing maven (https://maven.apache.org/install.html).
First step: going to Spring Initializr site
This site will build for us a Spring Boot app structure with everything we need (it is sufficient to search the dependencies that we need in the section 'Dependencies'). Let's click on 'ADD DEPENDENCIES' and add the dependencies displayed in the image.
The dependency of H2 database allows to working fast with a "in-memory" database (that is, after stop the app, the data will be deleted). Default is if we don't configure any database in the application.properties file of project and there is an H2 dependency, Spring Boot will set up automatically ad H2 database in-memory.
Let's click on 'Generate'. It will download the zip of the project. The project looks with this structure:
Second step: let's create an Entity who will mapped a table of the Database
Lt's create a subpackage entities e there we create a Java class who mapped a USER table:
@Entity
public class User implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String surname;
private String address;
//getter, setter, equals and hashcode
}
Third step: let's create a repository for the User entity
public interface UserRepository extends JpaRepository<User,Long> {
}
Yes, it's just an empty interface, but it's everything we need! In fact Spring will create a class that extends SimpleJpaRepository at runtime
who includes several methods like findAll (with pagination too), findById, save, delete.
Moreover, since we added in build phase the Spring Data REST dependency, every method of the Repository will be mapped
in a REST service of level 3 in the resource /users.
Finished! Let's run the app
Let's run the app by the main or build the project with maven e using the command java -jar spring-boot-rest.jar on the jar. Our web service REST is started on port 8080! Let's insert a user with the POST service localhost:8080/users with this JSON body:
{
"name": "John",
"surname": "Mayer",
"address": "via Roma"
}
The response will have 201 HTTP status (Created) with this response body:
{
"name": "John",
"surname": "Mayer",
"address": "via Roma",
"_links": {
"self": {
"href": "http://localhost:8080/users/1"
},
"user": {
"href": "http://localhost:8080/users/1"
}
}
}
Let's create another User in the same way, then we call the GET localhost:8080/users service who replies with the list of all user
saved in the database.
{
"_embedded": {
"users": [
{
"name": "John",
"surname": "Mayer",
"address": "via Roma",
"_links": {
"self": {
"href": "http://localhost:8080/users/1"
},
"user": {
"href": "http://localhost:8080/users/1"
}
}
},
{
"name": "Massimo",
"surname": "Esposito",
"address": "via Napoli",
"_links": {
"self": {
"href": "http://localhost:8080/users/2"
},
"user": {
"href": "http://localhost:8080/users/2"
}
}
}
]
},
"_links": {
"self": {
"href": "http://localhost:8080/users"
},
"profile": {
"href": "http://localhost:8080/profile/users"
}
},
"page": {
"size": 20,
"totalElements": 2,
"totalPages": 1,
"number": 0
}
}
We can try to call the GET localhost:8080/users/1 service for check just the information of user with id 1.
Let's try now to edit the address of user with id 1 calling the PUT a localhost:8080/users/1 service:
{
"name": "John",
"surname": "Mayer",
"address": "via Milano"
}
Finally, let's try to delete the user 1 calling the DELETE a localhost:8080/users/1 service.
We'll have the 204 HTTP response, because the Spring's delete don't give any response body.
Let's check that the user 1 is indeed deleted calling the GET su localhost:8080/users/1 service.
We will have the 404 HTTP response because the resource with id 1 don't exist more.
Conclusions
We built a RESTful Web Service of level 3 (that is, with Hypermedia-Driven) coding just for the Entity.
The DAOs and the REST services are autogenerated from Spring for us!
You can download the full project from my github in this link: Spring Boot Rest
Posts of Spring Framework: Spring
Recommended books about Spring:
- Pro Spring 5 (Spring from scratch a hero): https://amzn.to/3KvfWWO
- Pivotal Certified Professional Core Spring 5 Developer Exam: A Study Guide Using Spring Framework 5 (for Spring certification): https://amzn.to/3KxbJSC
- Pro Spring Boot 2: An Authoritative Guide to Building Microservices, Web and Enterprise Applications, and Best Practices (Spring Boot of the detail): https://amzn.to/3TrIZic