tencent cloud

Building Web Services: Using Spring Boot to Build the RESTful API
Last updated: 2025-10-11 14:26:39
Building Web Services: Using Spring Boot to Build the RESTful API
Last updated: 2025-10-11 14:26:39
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.

Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback