搭建 Web 服务:使用 Spring Boot 搭建 RESTful API

最近更新时间:2025-03-04 11:50:23

我的收藏
spring-boot-starter-web 是 Spring Boot 官方提供的一个 Starter,用于快速构建基于 Spring MVC 的 Web 应用程序。它整合了开发 Web 应用所需的常用依赖,并提供了自动配置功能,使开发者能够以最少的配置搭建 Web 服务,包括 RESTful 服务。本次实践将通过引入 spring-boot-starter-web 快速搭建一个 Web 服务,用于测试 Redis 相关组件的功能是否正常。

配置 spring-boot-starter-web 依赖

在 pom.xml 加入组件依赖,如下所示。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.7.0</version>
</dependency>

搭建 Web 服务

定义一个名为 RedisController 的 Spring Boot 控制器类,通过 HTTP 接口操作 Redis 数据库,支持字符串(set、get)、集合(sadd、srem、sismember)、哈希(hset、hget)、列表(lpush、rpush、lpop、rpop)以及有序集合(zadd、zrank)等常见数据结构的操作。以下是 RedisController.java 的代码示例:
package com.example.redismonitor;

import com.example.redismonitor.RedisInterface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/redis")
public class RedisController {
@Autowired
private RedisInterface redisService;

//- - - - - - - - - - - - - - - - - - - - - string - - - - - - - - - - - - - - - - - - - -
@PostMapping("/set")
public String setValue(@RequestParam String key, @RequestParam String value) {
redisService.set(key, value);
return "Value set successfully";
}

@GetMapping("/get")
public Object getValue(@RequestParam String key) {
return redisService.get(key);
}

//- - - - - - - - - - - - - - - - - - - - - set - - - - - - - - - - - - - - - - - - - -
@PostMapping("/sadd")
public String sadd(@RequestParam String key, @RequestParam Object value) {
redisService.sadd(key, value);
return "Value sadd successfully";
}

@GetMapping("/srem")
public Object srem(@RequestParam String key,@RequestParam Object... values) {
return redisService.srem(key, values);
}

@GetMapping("/sismember")
public Object sismember(@RequestParam String key,@RequestParam Object value) {
return redisService.sismember(key, value);
}

//- - - - - - - - - - - - - - - - - - - - - hash - - - - - - - - - - - - - - - - - - - -
@PostMapping("/hset")
public String hset(@RequestParam String key, @RequestParam String field, @RequestParam Object value) {
redisService.hset(key, field, value);
return "Value hset successfully";
}

@GetMapping("/hget")
public Object hget(@RequestParam String key, @RequestParam String field) {
return redisService.hget(key, field);
}

//- - - - - - - - - - - - - - - - - - - - - list - - - - - - - - - - - - - - - - - - - -
@PostMapping("/lpush")
public String lpush(@RequestParam String key, @RequestParam Object value) {
redisService.lpush(key, value);
return "Value lpush successfully";
}

@PostMapping("/rpush")
public String rpush(@RequestParam String key, @RequestParam Object value) {
redisService.rpush(key, value);
return "Value rpush successfully";
}

@GetMapping("/lpop")
public Object lpop(@RequestParam String key) {
return redisService.lpop(key);
}

@GetMapping("/rpop")
public Object rpop(@RequestParam String key) {
return redisService.rpop(key);
}
//- - - - - - - - - - - - - - - - - - - - - zset - - - - - - - - - - - - - - - - - - - -
@PostMapping("/zadd")
public String zadd(@RequestParam String key, @RequestParam Object value, @RequestParam double score) {
redisService.zadd(key, value, score);
return "Value zadd successfully";
}

@GetMapping("/zrank")
public Object zrank(@RequestParam String key, @RequestParam Object value) {
return redisService.zrank(key, value);
}
}

测试 Web 服务

通过 `curl` 命令调用已实现的 Web 服务接口,测试 Redis 的功能。以下示例展示了如何使用 `curl` 命令执行 SET 和 GET 操作。