tencent cloud

腾讯云分布式缓存数据库(兼容 Redis)

动态与公告
产品动态
公告
新手指引
产品简介
产品概述
产品优势
应用场景
存储引擎
产品系列
产品版本
规格与性能
读写分离
多可用区部署
地域和可用区
名词解释
购买指南
计费概述
定价中心
购买实例
续费说明(包年包月)
退费说明(包年包月)
欠费说明
按量转包年包月
快速入门
快速创建实例
连接 Redis 实例
操作指南
操作总览
连接数据库实例
管理实例
升级实例
管理节点(Redis/ValKey 版)
管理多可用区
备份与恢复
账号管理
参数配置
慢查询
访问管理
网络与安全
监控与告警
事件管理(Redis/ValKey 版)
数据迁移
Redis 版全球复制
数据库审计
诊断优化
Sentinel 模式
开发准则
命名规则
基本使用准则
Key 与 Value 设计原则
命令使用准则
客户端程序设计准则
连接池配置
命令参考
命令参考概览
Redis 版与 Valkey 版命令兼容性
大版本命令使用差异
Proxy 架构与直连模式的使用差异
命令更多操作(Redis/Valkey 版)
Memcached 版命令兼容性
实践教程
基于 Spring Boot 搭建 Redis 客户端监控
Redis 客户端连接配置策略与实践
集群架构全局 SCAN 使用指南
实例安全下线
热 Key 与 大 key
可用区迁移方案
故障处理
连接异常
Redisson 客户端超时重连异常分析及解决方案
性能排查与调优
API 文档
History
Introduction
API Category
Making API Requests
Instance APIs
Parameter Management APIs
Other APIs
Backup and Restoration APIs
Region APIs
Monitoring and Management APIs
Log APIs
Data Types
Error Codes
常见问题
使用常见问题
连接登录问题
购买相关问题
相关协议
服务等级协议
Terms of Service
词汇表
联系我们

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

PDF
聚焦模式
字号
最后更新时间: 2026-03-17 18:23:47
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 操作。


帮助和支持

本页内容是否解决了您的问题?

填写满意度调查问卷,共创更好文档体验。

文档反馈