:2026-03-10 16:00 点击:3
在区块链技术浪潮中,以太坊凭借其智能合约功能,成为了构建去中心化应用 的首选平台,对于许多开发者而言,如何将复杂的区块链交互逻辑与传统后端系统无缝对接,是一个不小的挑战,Spring Boot 作为 Java 生态中最流行的后端开发框架,以其简洁的配置、强大的生态和高效的开发体验,为解决这个问题提供了完美的答案。
本文将深入探讨如何将 Spring Boot 与以太坊网络集成,通过实际步骤和代码示例,展示如何使用 Spring Boot 作为后端服务,与以太坊节点进行通信、部署智能合约以及调用合约方法,为你的 DApp 坚实可靠的后端基石。
在开始之前,我们首先要明确为什么选择 Spring Boot 作为以太坊交互的中间层。
在开始编码之前,请确保你的开发环境已准备就绪:
我们将通过一个完整的示例,展示 Spring Boot 项目如何连接以太坊节点,并与一个简单的智能合约进行交互。
使用 Spring Initializr 创建一个新的 Maven 项目,并添加以下依赖:
Spring Web:用于创建 RESTful API。Lombok:简化 Java 代码,减少样板代码。我们选择目前最流行和功能最强大的以太坊 Java SDK——Web3j,在 pom.xml 文件中添加以下依赖:
<dependency>
<groupId>org.web3j</groupId>
<artifactId>core</artifactId>
<version>4.9.8</version> <!-- 请使用最新稳定版本 -->
</dependency>
<dependency>
<groupId>org.web3j</groupId>
<artifactId>geth</artifactId>
<version>4.9.8</version>
</dependency>
在 src/main/resources/application.properties 文件中,配置你的以太坊节点地址。
# 可选:指定使用的账户,用于发送交易
# 默认会使用节点中第一个可用的账户
# ethereum.account.address=0xYourAccountAddress
这是核心步骤,我们将创建一个服务类,负责与以太坊网络的所有交互。
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.methods.response.EthBlockNumber;
import org.web3j.protocol.core.methods.response.EthGetBalance;
import org.web3j.protocol.http.HttpService;
import org.web3j.utils.Convert;
import org.web3j.utils.Convert.Unit;
import java.math.BigDecimal;
import java.math.BigInteger;
@Service
public class EthereumService {
// 通过 @Value 注入配置文件中的节点 URL
@Value("${ethereum.node.url}")
private String nodeUrl;
// Web3j 实例,在服务启动时初始化
private Web3j web3j;
// 使用 @PostConstruct 在 Bean 初始化后执行
public void init() {
this.web3j = Web3j.build(new HttpService(nodeUrl));
System.out.println("Successfully connected to Ethereum node: " + nodeUrl);
}
/**
* 获取最新区块号
*/
public BigInteger getLatestBlockNumber() throws Exception {
EthBlockNumber ethBlockNumber = web3j.ethBlockNumber().send();
return ethBlockNumber.getBlockNumber();
}
/**
* 获取指定地址的以太币余额
* @param address 以太坊地址
*/
public BigDecimal getB
alance(String address) throws Exception {
EthGetBalance ethGetBalance = web3j.ethGetBalance(address, org.web3j.protocol.core.DefaultBlockParameterName.LATEST).send();
BigInteger weiBalance = ethGetBalance.getBalance();
// 将 Wei 转换为 Ether
return Convert.fromWei(new BigDecimal(weiBalance), Unit.ETHER);
}
}
我们将服务层暴露为 HTTP 接口,供前端调用。
import org.springframework.beans.factory.annotation.Autowired;
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.math.BigInteger;
@RestController
@RequestMapping("/api/eth")
public class EthereumController {
@Autowired
private EthereumService ethereumService;
@GetMapping("/blockNumber")
public BigInteger getBlockNumber() throws Exception {
return ethereumService.getLatestBlockNumber();
}
@GetMapping("/balance/{address}")
public String getBalance(@PathVariable String address) throws Exception {
BigDecimal balance = ethereumService.getBalance(address);
return "Balance of " + address + " is: " + balance.toPlainString() + " ETH";
}
}
运行你的 Spring Boot 应用。
使用 Postman 或浏览器访问以下 API:
GET http://localhost:8080/api/eth/blockNumber
你将看到当前连接的测试网的最新区块号。GET http://localhost:8080/api/eth/balance/0xYourTestAccountAddress
将 0xYourTestAccountAddress 替换为你在测试网上的一个地址,你将看到该地址的 ETH 余额。至此,你已经成功地将 Spring Boot 与以太坊节点连接起来了!
与节点交互只是基础,真正的 DApp 核心在于与智能合约的交互,以下是高级步骤:
假设你有一个简单的 SimpleStorage.sol 合约:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract SimpleStorage {
uint256 private myNumber;
function set(uint256 _newNumber) public {
myNumber = _newNumber;
}
function get() public view returns (uint256) {
return myNumber;
}
}
使用 Web3j 命令行工具为其生成 Java 包装类:
web3j solidity generate -a SimpleStorage.bin -b SimpleStorage.abi -p com.yourpackage.contracts -o src/main/java
这会在 src/main/java/com/yourpackage/contracts 目录下生成 SimpleStorage.java 等文件。
修改 EthereumService,添加加载合约和调用合约的方法。
// ... 其他 imports import org.web3j.protocol.core.methods.response.TransactionReceipt; import org.web3j.tx.g
本文由用户投稿上传,若侵权请提供版权资料并联系删除!