Spring Boot 指南
学习地址:https://snailclimb.gitee.io/springboot-guide/
RESTful Web 服务
新建 SpringBoot 项目
依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <!-- 需要下载 IDEA 中支持 lombok 的插件 --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency>
相关注解
@Controller 返回一个页面
@ResponseBody 数据直接以 JSON 或 XML 形式返回
@RestController = @Controller + @ResponseBody
@PathVariable 地址参数
@RequestParam 查询参数
@RequestBody body 中的 JSON 类型数据反序列化为合适的 Java 类型
实体类
package com.example.demo.entity;
import lombok.Data;
@Data
public class Book {
private String name;
private String description;
}
控制器
package com.example.demo.controller; import com.example.demo.entity.Book; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; @RestController @RequestMapping("/api") public class BookController { private List<Book> books = new ArrayList<>(); // 获取列表 @GetMapping("/books") public ResponseEntity<List<Book>> list() { return ResponseEntity.ok(this.books); } // 获取单个 @GetMapping("/book/{id}") public ResponseEntity<Book> getBook(@PathVariable("id") int id) { return ResponseEntity.ok(this.books.get(id)); } // 查询数据 @GetMapping("/book") public ResponseEntity<List<Book>> getBookByName(@RequestParam("name") String name) { List<Book> result = this.books.stream() .filter(book -> book.getName().equals(name)) .collect(Collectors.toList()); return ResponseEntity.ok(result); } // 删除数据 @DeleteMapping("/book/{id}") public ResponseEntity<Boolean> deleteBook(@PathVariable("id") int id) { this.books.remove(id); return ResponseEntity.ok(true); } // 添加数据 @PostMapping("/book") public ResponseEntity<Book> addBook(@RequestBody Book book) { this.books.add(book); return ResponseEntity.ok(book); } }
测试
http/http-client.private.env.json
{ "dev": { "BASE_URL": "http://localhost:8080" } }
http/books.http
GET {{BASE_URL}}/api/books ### POST {{BASE_URL}}/api/book content-type: application/json { "name": "《三国演义》", "description": "一本书" } ### POST {{BASE_URL}}/api/book content-type: application/json { "name": "《西游记》", "description": "第2本书" } ### POST {{BASE_URL}}/api/book content-type: application/json { "name": "《红楼梦》", "description": "第3本书" } ### GET {{BASE_URL}}/api/book/1 ### GET {{BASE_URL}}/api/book?name=《红楼梦》 ### DELETE {{BASE_URL}}/api/book/0
返回视图
添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
控制器
package com.example.demo.controller; package com.example.demo.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller public class HelloController { @GetMapping("/hello") public String hello(@RequestParam(name = "name", required=false, defaultValue = "World") String name, Model model){ model.addAttribute("name", name); return "hello"; } }
视图文件
templates/hello.html
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8" /> <title>Title</title> </head> <body> <h2 th:text="'hello ' + ${name}"></h2> </body> </html>
http://localhost:8080/hello
返回
hello World