How to build Restful Service with Spring Boot - A Step by Step Tutorial for Beginner - 1 (Update to Spring Boot 2.6 and Java 17)

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
2
3
4
5
6
7
8
9
10
11
12
13
todo-restful-service
+ .mvn
+ src
+ main
+ java
+ resources
+ test
+ java
.gitignore
HELP.md
mvnw
mvnw.cmd
pom.xml

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
2
3
4
5
6
7
8
9
10
@RestController
@RequestMapping("/")
public class OkApi {

@GetMapping
public String ok() {
return "ok";
}

}

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
2
3
4
5
6
7
8
9
public class Todo {

private Integer id;

private String title;

private String desc;

}

In the class, we have neither written get, set methods nor constructors. Because we will use Lombok to generate these.

  1. If you need to generate get, set methods, add @Data annotations to the class definition
  2. 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
2
3
4
5
6
7
8
9
10
11
@Data
@AllArgsConstructor
public class Todo {

private Integer id;

private String title;

private String desc;

}

Second, let’s create a new RESTful service class, named: TodoApi. It will return all Todo (Resources) lists. The code is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@RestController
@RequestMapping("/todo")
public class TodoApi {

@GetMapping
public List<Todo> getTodoList() {

List<Todo> todos = new ArrayList<>();
todos.add(new Todo(1, "Call Meeting", ""));
todos.add(new Todo(2, "Print file", ""));

return todos;
}
}

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.

本文标题:How to build Restful Service with Spring Boot - A Step by Step Tutorial for Beginner - 1 (Update to Spring Boot 2.6 and Java 17)

文章作者:Morning Star

发布时间:2022年01月25日 - 11:01

最后更新:2022年01月25日 - 15:01

原始链接:https://www.mls-tech.info/java/howto-build-restful-service-with-spring-boot/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。