REST 控制器是用于构建基于 REST 架构风格的 Web 服务的一部分。REST(Representational State Transfer)是一种基于 HTTP 协议的网络架构风格,它定义了一组约束和属性,通过使用一小组标准操作(如 GET、POST、PUT、DELETE)进行数据操作。Spring Boot 提供了方便的注解和工具,使得创建 RESTful Web 服务变得非常简单。

REST 控制器的核心概念

  1. 资源(Resource)
    • 在 REST 架构中,资源是网络上的实体,通常通过 URI(统一资源标识符)来标识。每个资源都有一个唯一的 URI。
  2. HTTP 方法
    • REST 使用标准的 HTTP 方法来执行操作。常用的 HTTP 方法有:
      • GET:从服务器获取资源。
      • POST:向服务器创建一个新的资源。
      • PUT:更新服务器上的资源。
      • DELETE:删除服务器上的资源。
  3. 表示(Representation)
    • 资源的表示是将资源的状态传输给客户端的一种方式,通常是 JSON 或 XML 格式。
  4. 无状态(Stateless)
    • 每个请求都必须包含处理该请求所需的所有信息,服务器不应该在两个请求之间存储客户端的状态。

Spring Boot 中的 REST 控制器

在 Spring Boot 中,REST 控制器是一个带有 @RestController 注解的类,它处理 HTTP 请求并返回响应。该类中的每个方法都可以映射到一个特定的 HTTP 请求路径和方法。

关键注解

  • @RestController
    • 标记一个类为 Spring MVC 的控制器,并且每个方法返回的对象会被自动转换为 JSON 格式,直接写入 HTTP 响应体。
  • @GetMapping
    • 映射 HTTP GET 请求到指定的方法。
  • @PostMapping
    • 映射 HTTP POST 请求到指定的方法。
  • @PutMapping
    • 映射 HTTP PUT 请求到指定的方法。
  • @DeleteMapping
    • 映射 HTTP DELETE 请求到指定的方法。

示例代码

下面是一个简单的 Spring Boot REST 控制器示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.example.demo;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.List;

@RestController
public class ItemController {

private List<String> items = new ArrayList<>();

@GetMapping("/items")
public List<String> getItems() {
return items;
}

@PostMapping("/items")
public String addItem(@RequestBody String item) {
items.add(item);
return "Item added";
}

@PutMapping("/items/{index}")
public String updateItem(@PathVariable int index, @RequestBody String newItem) {
if (index >= 0 && index < items.size()) {
items.set(index, newItem);
return "Item updated";
} else {
return "Item not found";
}
}

@DeleteMapping("/items/{index}")
public String deleteItem(@PathVariable int index) {
if (index >= 0 && index < items.size()) {
items.remove(index);
return "Item deleted";
} else {
return "Item not found";
}
}
}

代码讲解

  • 类和包
    • package com.example.demo; 声明类的包路径。
    • public class ItemController 定义了一个名为 ItemController 的公共类。
  • 注解
    • @RestController 表明该类是一个 REST 控制器,方法返回的对象会自动转换为 JSON。
  • 字段
    • private List<String> items = new ArrayList<>(); 定义了一个存储字符串的列表,用来模拟数据存储。
  • 方法
    • @GetMapping("/items"):处理 GET 请求,返回所有项目。
    • @PostMapping("/items"):处理 POST 请求,接收请求体中的数据并添加到 items 列表中。
    • @PutMapping("/items/{index}"):处理 PUT 请求,更新指定索引的项目。
    • @DeleteMapping("/items/{index}"):处理 DELETE 请求,删除指定索引的项目。

通过这些示例和讲解,你应该能够理解 REST 控制器的基本概念以及如何在 Spring Boot 中实现一个简单的 RESTful API。