This is a step by step tutorail for beginner, you can learn how to use spring boot to build a restful service (api). And we also show you: how to connect DB with spring data, how to validate user input with hibernate validation, and how to document your api (restful service) with Swagger.
Development Environment
In tutorial, we use the following softeware environment, and all softwares are free.
Java: JDK 17 Oracle JDK 17, download it from Offical Website
IDE: IntelliJ IDEA 2021.3.1 Community, download it from Offical Website
Generate Project Skeleton
In Spring Boot Starter, fill in the basic configuration information(Artifact: todo-restful-service) of the project and the third-party components that need to be used in the project.
In this turorail, we suggest to choose:
- ‘Java’ as the language
- ‘Maven Project’ as the Project (type)
- the version of Spring Boot chooses the latest stable version 2.6.2.
- ‘Jar’ as the package (type)
- the version of Java choose Java 17
And choose the following dependencies:
- Lombok
- Spring Web
- Spring Data JPA
- H2 Database
After completed the above steps, click the “Generate” button to download the generated project basic package.
We get a file named: todo-restful-service.zip, unzip it and use IDEA to open the project directory. Because there is a pom.xml file in the directory, IDEA can detect that this is a maven project and automatically download the dependencies packages.
Because the project is Maven project, we can see the project structure as following:
1 | todo-restful-service |
In this tutorial, we foucs on the ‘src’ folder, because the code we will write or modify is stored in it.
Create the first RESTful Service
In Spring Boot, create an RESTful service is very simple:
- First, create a POJO Java class
- Second, add two annotations(RestController, RequestMapping) for class
- Define a common method that returns a string in the class and add annotation(GetMapping) for this method.
The completed code as below:
1 |
|
Now, run the program and visit the address
1 | http://localhost:8080 |
then you can see the “ok” is display in browser.
Add a Todo Service
In this chapter, we create an new service: Todo. It is used to expose the ‘Todo’ resource.
First, let’s create a new ‘model’ package in the project, and then create a POJO Java class in it, named: Todo, add a few basic attributes to the class:
1 | public class Todo { |
In the class, we have neither written get, set methods nor constructors. Because we will use Lombok to generate these.
- If you need to generate get, set methods, add @Data annotations to the class definition
- If you need to generate a constituent function, use the annotation of the corresponding constructor. In this example, we construct a constructor that includes all attributes, then add @AllArgsConstructor
The completed code is as follows:
1 |
|
Second, let’s create a new RESTful service class, named: TodoApi. It will return all Todo (Resources) lists. The code is as follows:
1 |
|
Here, we map the method to the URL “/todo” and construct Todo data for two examples. If the error “Todo does not specify the composition method” appears after joining this program, you can refer to How to enable lombok annotations in IDEA IDE to solve
Now, run the program and visit the address
1 | localhost:8080/todo |
, you can see that the Todo list is assembled into a json data format and returned.
1 | [{"id":1,"title":"Call Metting","desc":""},{"id":2,"title":"Print File","desc":""}] |
Next Step
In next post, we will add database functionality to the application.