Maven SpringBoot Application Example¶
Tags: “spring-boot-crud” “development of blockchain applications”
This sample project is based on the Java SDK+ Maven + SpringBoot way to invoke smart contracts。
If you want to use the Java SDK+ Gradle + To access smart contracts in SpringBoot mode, please refer to Gradle example
Preconditions¶
To build a FISCO BCOS single-group blockchain (Air version), the specific steps refer here。
Get source code¶
# Clone code directly from github
git clone https://github.com/FISCO-BCOS/spring-boot-crud.git
# If the network is slow, clone the code from gitee
git clone https://gitee.com/FISCO-BCOS/spring-boot-crud
Configure Node Certificates¶
Directory where the node is located ‘nodes / ${ip}Copy the ca.crt, sdk.crt, and sdk.key files under / sdk ‘to the project’s’ src / main / resources / conf ‘directory for SDK use(Prior to FISCO BCOS 2.1, the certificates were ca.crt, node.crt, and node.key):
Set the node path to ‘~ / fisco / nodes / 127.0.0.1’. You can use the following command to copy the SDK certificate:
# Enter project path
$ cd spring-boot-crud
# Create a certificate storage path
$ mkdir src/main/resources/conf
# Copy SDK certificate
$ cp ~/fisco/nodes/127.0.0.1/sdk/* src/main/resources/conf/
Setting Up Configuration Files¶
‘spring-boot-crud ‘includes SDK configuration files (located in the’ src / main / resources / applicationContext.xml ‘path) and WebServer configuration files(Located in the ‘src / main / resources / application.yml’ path)。
You need to configure the ‘network.peers’ configuration item of ‘applicationContext.xml’ based on the IP address and port of the blockchain node, as follows:
...
<property name="network">
<map>
<entry key="peers">
<list>
<value>127.0.0.1:20200</value>
<value>127.0.0.1:20201</value>
</list>
</entry>
<entry key="defaultGroup" value="group0" />
</map>
</property>
...
Please refer to [here] for detailed instructions on SDK configuration in the project(https://fisco-bcos-documentation.readthedocs.io/zh_CN/latest/docs/sdk/java_sdk/configuration.html)。
WebServer is mainly configured with a listening port, which is’ 45000 ‘by default, as follows:
server:
#Port number
port: 45000
Compiling and installing projects¶
You can use IDEA to import and compile and install the project, or you can use the provided ‘mvnw’ script to compile the project at the command line as follows:
# Compile Project
$ bash mvnw compile
# Install the project. After installation, generate the jar package of fisco-bcos-spring-boot-crud-0.0.1-SNAPSHOT.jar in the target / directory
$ bash mvnw install
Start the spring-boot-crud service¶
Method one:
Open IDEA to import and compile the project. After successful compilation, run ‘AppApplication.java’ to start the spring boot service。
Method two:
Use the ‘bash mvnw install’ generated jar package ‘target / fisco-bcos-spring-boot-crud-0.0.1-SNAPSHOT.jar’ to start the spring-boot-crud service:
# Start spring-boot-crud(After successful startup, the log of create client for group 1 success will be output)
$ java -jar ./target/fisco-bcos-spring-boot-crud-0.0.1-SNAPSHOT.jar
Accessing the spring boot web service¶
Access user information on the chain API(KV set)¶
‘spring-boot-crud ‘implements the user information chaining API based on the KV set interface, and chaining the user information of the’ Person ‘type. The API statement is as follows:
@Data
public class Person {
private String name;
private String age;
private String tel;
}
@PostMapping("/set")
public ResponseData set(@RequestBody Person person) {
kvClient.set(person.getName(), person.getAge(), person.getTel());
return ResponseData.success("Added successfully");
}
Use the curl tool to access the interface as follows:
# It is assumed here that the WebServer listening port is 45000
# Chain the information of user fisco, where name is fisco, age is 6, and tel is 123456789
$ curl -H "Content-Type: application/json" -X POST --data '{"name":"fisco", "age":"6", "tel":"123456789"}' http://localhost:45000/set
# Return the information of successful addition
{"code":200,"msg":"Added successfully","data":null}
Query user information API on access chain(KV get)¶
‘spring-boot-crud ‘implements an API for querying user information on the chain based on the KV get interface. The API is declared as follows:
@GetMapping("/get/{name}")
public ResponseData get(@PathVariable("name") String name) throws Exception {
return ResponseData.success(kvClient.get(name));
}
Use the curl tool to access the interface as follows:
# It is assumed here that the WebServer listening port is 45000
# Querying User Information for a User Name of fisco
$ curl http://localhost:45000/get/fisco
# returns specific information about the user fisco
{"code":200,"msg":null,"data":{"value1":"fisco","value2":"6","value3":"123456789","size":3}}
Note: The current version does not support the CRUD interface of Table, only the function of KV interface is provided。CRUD features will be supported in the next release。
Contribution code¶
We welcome and greatly appreciate your contribution, see Code Contribution Process。 -If the project is helpful to you, welcome star support!
Join us¶
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。
