You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求指导:用Spring Boot创建REST API连接已有GCP Spanner数据库的初始步骤

Spring Boot REST API 连接 GCP Spanner 初始步骤

1. 搭建Spring Boot项目基础

  • 用Spring Initializr初始化项目时,勾选Spring Web模块;或者手动在构建文件中添加依赖:

Maven(pom.xml)

<dependencies>
    <!-- Spring Web 用于REST API开发 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- GCP Spanner Spring集成依赖 -->
    <dependency>
        <groupId>com.google.cloud</groupId>
        <artifactId>spring-cloud-gcp-starter-data-spanner</artifactId>
        <version>3.4.1</version> <!-- 替换为最新稳定版 -->
    </dependency>
</dependencies>

Gradle(build.gradle)

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'com.google.cloud:spring-cloud-gcp-starter-data-spanner:3.4.1'
}

2. 配置Spanner连接信息

在src/main/resources/application.properties(或application.yml)中添加数据库连接配置:

# GCP项目ID
spring.cloud.gcp.project-id=你的GCP项目ID
# Spanner实例ID
spring.cloud.gcp.spanner.instance-id=你的Spanner实例ID
# Spanner数据库ID
spring.cloud.gcp.spanner.database-id=你的数据库ID

本地开发权限配置

本地运行时,需要设置环境变量指向GCP服务账号密钥文件:

# Linux/macOS终端
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/your/service-account-key.json

# Windows PowerShell
$env:GOOGLE_APPLICATION_CREDENTIALS="C:\path\to\your\service-account-key.json"

3. 映射Spanner表到实体类

假设你有一张users表,编写对应的实体类:

import com.google.cloud.spring.data.spanner.core.mapping.Column;
import com.google.cloud.spring.data.spanner.core.mapping.PrimaryKey;
import com.google.cloud.spring.data.spanner.core.mapping.Table;

@Table(name = "users")
public class User {
    @PrimaryKey
    @Column(name = "id")
    private String id;

    @Column(name = "name")
    private String name;

    @Column(name = "email")
    private String email;

    // 无参构造函数、全参构造函数、getter/setter(自行补充)
    public User() {}

    public User(String id, String name, String email) {
        this.id = id;
        this.name = name;
        this.email = email;
    }

    // getter和setter方法省略,按需添加
}

4. 创建数据访问Repository

继承Spring Data Spanner的SpannerRepository,快速实现CRUD操作:

import com.google.cloud.spring.data.spanner.repository.SpannerRepository;
import org.springframework.stereotype.Repository;
import java.util.List;

@Repository
public interface UserRepository extends SpannerRepository<User, String> {
    // 自定义查询示例:根据用户名查询
    List<User> findByName(String name);
}

5. 编写测试用REST接口

创建Controller暴露接口,验证数据库连接:

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

@RestController
@RequestMapping("/users")
public class UserController {
    private final UserRepository userRepository;

    // 构造函数注入Repository
    public UserController(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    @GetMapping
    public List<User> getAllUsers() {
        return userRepository.findAll();
    }

    @GetMapping("/{id}")
    public User getUserById(@PathVariable String id) {
        return userRepository.findById(id).orElse(null);
    }
}

6. 启动并验证连接

  • 启动Spring Boot应用,确保环境变量已正确配置服务账号密钥。
  • 调用GET http://localhost:8080/users接口,若能返回数据库中的用户数据,说明连接成功。

内容的提问来源于stack exchange,提问作者Jay93

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 21:52:55