tencent cloud

Tencent Cloud Distributed Cache (Redis OSS-Compatible)

Release Notes and Announcements
Release Notes
Announcements
User Tutorial
Product Introduction
Overview
Product Strengths
Use Cases
Storage Engine
Product Series
Product Versions
Specifications and Performance
Read/Write Separation
Multi-AZ Deployment
Regions and AZs
Terms
Service Regions and Service Providers
Purchase Guide
Billing Overview
Pricing Center
Instance Purchasing
Renewal (Yearly/Monthly Subscription)
Refund (Yearly/Monthly Subscription)
Overdue Payments
Switching from Pay-as-You-Go to Yearly/Monthly Subscription
Getting Started
Quickly Creating an Instance
Connecting to Redis Instance
Operation Guide
Operation Overview
Connecting to a Database Instance
Managing Instances
Upgrade Instance
Management Node (Redis/ValKey Edition)
Multi-AZ Deployment Management
Backup and Restoration
Managing Accounts
Parameter Configuration
Slow Query
Access Management
Network and Security
Monitoring and Alarms
Event Management (Redis/ValKey Edition)
Data Migration
Global Replication for Redis Edition
Database Audit
Performance Optimization
Sentinel Mode
Development Guidelines
Naming Rules
Basic Usage Guidelines
Design Principles of Key and Value
Command Usage Guidelines
Design Principles of Client Programs
Connection Pool Configuration
Command Reference
Command Reference Overview
Redis Edition and Valkey Edition Command Compatibility
Version Command Usage Differences
Differences Between the Proxy Architecture and Direct Connection Mode
More Command Operations (Redis/Valkey Edition)
Memcached Edition Command Compatibility
Practical Tutorial
Building TencentDB for Redis® Client Monitoring Based on Spring Boot
Redis Client Connection Configuration Policy and Practice
Global SCAN Guide for Cluster Architecture
Eliminating Instances Securely
Hot Key and Big Key
AZ Migration Scheme
Troubleshooting
Connection Exception
Exception Analysis and Solution of Redisson Client Timeout Reconnection
Performance Troubleshooting and Fine-Tuning
API Documentation
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
FAQs
General
Connection and Login
Purchase
Service Agreement
Service Level Agreement
Terms of Service
Glossary
Contact Us

Building Web Services: Using Spring Boot to Build the RESTful API

PDF
Modo Foco
Tamanho da Fonte
Última atualização: 2026-03-17 18:23:48
spring-boot-starter-web is a starter officially provided by Spring Boot for quickly building web applications based on Spring MVC. It integrates commonly used dependencies needed for web application development and provides automatic configuration features, allowing developers to build web services, including RESTful services, with minimal configuration. This practice will quickly build a web service by introducing spring-boot-starter-web to test the functionality of TencentDB for Redis®-related components.

Configuring the spring-boot-starter-web Dependency

Add the component dependency to pom.xml as follows.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.7.0</version>
</dependency>

Building Web Services

Define a Spring Boot controller class named RedisController that provides HTTP APIs for operating TencentDB for Redis®. It supports operations on common data structures, such as strings (set and get), sets (sadd, srem, and sismember), hash commands (hset and hget), lists (lpush, rpush, lpop, and rpop), and ordered sets (zadd and zrank). The following is an example code for 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);
}
}

Testing Web Services

Run the `curl` command to call the implemented web service APIs and test the functionality of TencentDB for Redis®. The following example demonstrates how to perform SET and GET operations by running the `curl` command.


Ajuda e Suporte

Esta página foi útil?

comentários