tencent cloud

Tencent Cloud Distributed Cache (Redis OSS-Compatible)

Webサービスの構築:Spring Bootを使用したRESTful APIの構築

ダウンロード
フォーカスモード
フォントサイズ
最終更新日: 2026-10-20 19:07:17
AI翻訳
spring-boot-starter-web は、Spring Bootが公式に提供するStarterであり、Spring MVCベースのWebアプリケーションを迅速に構築するために使用されます。これは、Webアプリケーション開発に必要な一般的な依存関係を統合し、自動構成機能を提供するため、開発者は最小限の構成でRESTfulサービスを含むWebサービスを構築できます。今回の実践では、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操作を実行する方法を示します。


ヘルプとサポート

この記事はお役に立ちましたか?

フィードバック