Gradle SpringBoot Application Example¶
Tags: “spring-boot-starter” “development of blockchain applications”
This sample project is based on the Java SDK+ Gradle + SpringBoot way to invoke smart contracts。
If you want to use the Java SDK+ Maven + To access smart contracts in SpringBoot mode, please refer to Maven Example
Preconditions¶
To build a FISCO BCOS single-group blockchain (Air version), the specific steps refer here。
Download spring-boot-starter, certificate copy¶
Note
If accessing github is slow, you can try to clone the code from gitee. The gitee link is https://gitee.com/FISCO-BCOS/spring-boot-starter.git
git clone https://github.com/FISCO-BCOS/spring-boot-starter.git
Enter the spring-boot-starter project
cd spring-boot-starter
Copy the certificate to the src / main / resources / conf directory。
Configure Connection Node¶
Modify application.properties, which contains the following information:
### Java sdk configuration
cryptoMaterial.certPath=conf
network.peers[0]=127.0.0.1:20200
#network.peers[1]=127.0.0.1:20201
### System configuration
system.groupId=group0
system.hexPrivateKey=
### Springboot configuration
server.port=8080
Among them:
Java SDK configuration configuration section with Java SDKConsistent。For this example, the user needs to:
replace network.peers with the actual listening address of the chain node。 -cryptoMaterial.certPath is set to conf
-System configuration configuration configuration section, you need to configure: -system.hexPrivateKey is the clear text of the hexadecimal private key, which can be generated by running ‘keyGeneration’ in Demos.java (file path: src / test / java / org / example / demo / Demos.java)。The configuration is allowed to be empty. In this case, the system randomly generates a private key。 -system.groupId is set to the target group. The default value is group0
The Demos.java code is as follows: (The latest project documents shall prevail)
package org.example.demo;
import java.util.Arrays;
import org.example.demo.constants.ContractConstants;
import org.fisco.bcos.sdk.v3.client.Client;
import org.fisco.bcos.sdk.v3.crypto.keypair.CryptoKeyPair;
import org.fisco.bcos.sdk.v3.crypto.keypair.ECDSAKeyPair;
import org.fisco.bcos.sdk.v3.crypto.keypair.SM2KeyPair;
import org.fisco.bcos.sdk.v3.model.TransactionReceipt;
import org.fisco.bcos.sdk.v3.transaction.manager.AssembleTransactionProcessor;
import org.fisco.bcos.sdk.v3.transaction.manager.TransactionProcessorFactory;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest
@RunWith(SpringRunner.class)
public class Demos {
@Autowired private Client client;
@Test
public void keyGeneration() throws Exception {
// ECDSA key generation
CryptoKeyPair ecdsaKeyPair = new ECDSAKeyPair().generateKeyPair();
System.out.println("ecdsa private key :" + ecdsaKeyPair.getHexPrivateKey());
System.out.println("ecdsa public key :" + ecdsaKeyPair.getHexPublicKey());
System.out.println("ecdsa address :" + ecdsaKeyPair.getAddress());
// SM2 key generation
CryptoKeyPair sm2KeyPair = new SM2KeyPair().generateKeyPair();
System.out.println("sm2 private key :" + sm2KeyPair.getHexPrivateKey());
System.out.println("sm2 public key :" + sm2KeyPair.getHexPublicKey());
System.out.println("sm2 address :" + sm2KeyPair.getAddress());
}
@Test
public void deploy() throws Exception {
AssembleTransactionProcessor txProcessor =
TransactionProcessorFactory.createAssembleTransactionProcessor(
client, client.getCryptoSuite().getCryptoKeyPair());
String abi = ContractConstants.HelloWorldAbi;
String bin = ContractConstants.HelloWorldBinary;
TransactionReceipt receipt =
txProcessor.deployAndGetResponse(abi, bin, Arrays.asList()).getTransactionReceipt();
if (receipt.isStatusOK()) {
System.out.println("Contract Address:" + receipt.getContractAddress());
} else {
System.out.println("Status code:" + receipt.getStatus() + "-" + receipt.getMessage());
}
}
}
Compile and run¶
You can run it directly in the idea, or you can compile it into an executable jar package and run it。To compile the jar package as an example:
cd spring-boot-starter
bash gradlew bootJar
cd dist
Spring-boot-starter-exec.jar is generated in the dist directory. You can execute this jar package:
java -jar spring-boot-starter-exec.jar
You can then access the relevant interfaces。
Set example:
curl http://127.0.0.1:8080/hello/set?n=hello
Return example (representing transaction hash):
0x1c8b283daef12b38632e8a6b8fe4d798e053feb5128d9eaf2be77c324645763b
Get example:
curl http://127.0.0.1:8080/hello/get
Return example:
["hello"]
Join our community¶
FISCO BCOS Open Source CommunityIt is an active open source community in China, which has long provided all kinds of support and assistance to institutional and individual developers。Thousands of technology enthusiasts from various industries have been researching and using FISCO BCOS。If you are interested in FISCO BCOS open source technology and applications, welcome to join the community for more support and help。
