Spring Boot使用Webase front的api
1.准备工作
一.搭建好fisco bcos区块链, webase-front中间平台,这里就请自行百度
二.准备好一个spring boot项目或者一个maven项目
2.pox.xml的所需要的依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.70</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.1.0</version>
</dependency>
application.yml的配置
server:
port: 5023
3.使用webase-front Api
使用url+abi参数的http请求来进行区块的交互
一… 我们使用的是webasefront的: 交易处理接口
此接口为WeBASE-Front使用本地私钥(页面中的测试用户)进行签名
通过合约信息进行调用,前置根据调用的合约方法是否是“constant”方法区分返回信息,“constant”方法为查询,返回要查询的信息。非“constant”方法为发送数据上链,返回块hash、块高、交易hash等信息。
方法入参(funcParam)为JSON数组,多个参数以逗号分隔(参数为数组时同理),示例:
二 .使用到接口URL地址:
http://localhost:5002/WeBASE-Front/trans/handle
三.调用方法
HTTP POST
四.需要的参数abi
1)参数表
序号 | 中文 | 参数名 | 类型 | 最大长度 | 必填 | 说明 |
---|---|---|---|---|---|---|
1 | 用户地址 | user | String | 是 | 用户地址,可通过/privateKey 接口创建 | |
2 | 合约名称 | contractName | String | 是 | ||
3 | 合约地址 | contractAddress | String | 是 | ||
4 | 方法名 | funcName | String | 是 | ||
5 | 合约编译后生成的abi文件内容 | contractAbi | List | 是 | 合约中单个函数的ABI,若不存在同名函数可以传入整个合约ABI,格式:JSONArray | |
6 | 方法参数 | funcParam | List | 否 | JSON数组,多个参数以逗号分隔(参数为数组时同理),如:[“str1”,[“arr1”,“arr2”]],根据所调用的合约方法判断是否必填 | |
7 | 群组ID | groupId | int | 是 | 默认为1 | |
8 | 合约路径 | contractPath | int | 否 | ||
9 | 是否使用cns调用 | useCns | bool | 是 | ||
10 | cns名称 | cnsName | String | 否 | CNS名称,useCns为true时不能为空 | |
11 | cns版本 | version | String | 否 | CNS版本,useCns为true时不能为空 |
2)数据格式
传入合约abi:
{
"groupId": "1",
"user": "0x7bfa3539c8761978f0a2a1f7f240bde97819fb03",
"contractName": "HelloWorld",
"contractPath": "/",
"version": "",
"funcName": "set",
"funcParam": ["333"],
"contractAddress": "0xe10441d9179cf0424aae808b51bc85dcbbfe1447",
"contractAbi": [{
"inputs": [{
"internalType": "string",
"name": "n",
"type": "string"
}],
"name": "set",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function",
"funcId": 2
}],
"useAes": false,
"useCns": false,
"cnsName": ""
}
3)创建java 实体类接受abi参数
@Data
@Slf4j
@ToString
@Component
public class WebaseFromcat {
private String name;
private String address;
private String abi;
private String funcName;
private String[] funcParam;
}
4)url详细abi参数,可以前面创建一个接口, 也可以删掉
@Component
public class WebaseUrlImpl implements WebaseUrl {
//当请求webase-front 中调用接口必须带有的参数
@Override
public String UrlHandle(WebaseFromcat webaseFromcat) {
String body = "{\"contractName\": \""+webaseFromcat.getName()+"\"," +
" \"contractAddress\": \""+webaseFromcat.getAddress()+"\", " +
"\"contractAbi\":"+webaseFromcat.getAbi()+","+
" \"user\": \"你测试用户的地址\"," +
" \"funcName\": \""+webaseFromcat.getFuncName()+"\"," +
" \"funcParam\": [";
//测试填入数组是否能够进行正常问数据
if(webaseFromcat.getFuncParam() != null){
String[] Param = webaseFromcat.getFuncParam();
body += "\"";
for(int i = 0; i < Param.length; i++){
if(i == (Param.length-1)) {
body += Param[i];
}else{
body += Param[i] + ",";
}
}
body += "\"";
}
return body+"]}";
}
}
5).返回参数:
{
"transactionHash": "0x69ced0162a0c3892e4eaa3091b831ac3aaeb772c062746b20891ceaf8a4fb429",
"transactionIndex": "0x0",
"root": "0x8cbc3f2c0e35a71738909e3b388efa6697084b05badd3a3bd3c64f0575c78c15",
"blockNumber": "2",
"blockHash": "0xf58f4f43b3761f4863ad366c4a7e2a812ed68df9f7bcad6b502fd544665e7625",
"from": "0x9d75e0ee66cfef16897b601624b60413d988ae7d",
"to": "0x0000000000000000000000000000000000000000",
"gasUsed": "316449",
"contractAddress": "0xa8af0ee580d8af674a60341030ddbc45431bc235",
"logs": [],
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"status": "0x0",
"statusMsg": "None",
"input": "0x608060405234801561001057600080fd5b506103e3806100206000396000f300608060405260043610610057576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063299f7f9d1461005c5780633590b49f146100ec57806362e8d6ce14610155575b600080fd5b34801561006857600080fd5b5061007161016c565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100b1578082015181840152602081019050610096565b50505050905090810190601f1680156100de5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b3480156100f857600080fd5b50610153600480360381019080803590602001908201803590602001908080601f016020809104026020016040519081016040528093929190818152602001838380828437820191505050505050919291929050505061020e565b005b34801561016157600080fd5b5061016a6102c4565b005b606060008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102045780601f106101d957610100808354040283529160200191610204565b820191906000526020600020905b8154815290600101906020018083116101e757829003601f168201915b5050505050905090565b7f5715c9562eaf8d524d564edb392acddefc81d8133e2fc3b8125a260b1b413fda816040518080602001828103825283818151815260200191508051906020019080838360005b83811015610270578082015181840152602081019050610255565b50505050905090810190601f16801561029d5780820380516001836020036101000a031916815260200191505b509250505060405180910390a180600090805190602001906102c0929190610312565b5050565b6040805190810160405280600d81526020017f48656c6c6f2c20576f726c6421000000000000000000000000000000000000008152506000908051906020019061030f929190610312565b50565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061035357805160ff1916838001178555610381565b82800160010185558215610381579182015b82811115610380578251825591602001919060010190610365565b5b50905061038e9190610392565b5090565b6103b491905b808211156103b0576000816000905550600101610398565b5090565b905600a165627a7a72305820f3088deb3d14c6893440e4769f2389e9335e04faa10e6de5b4c93af15d1a34e80029",
"output": "0x",
"txProof": null,
"receiptProof": null,
"message": null,
"statusOK": true
}
6)创建返回参数实体,用于接受返回参数, 可根据自己所需要的参数设置实体类的字段
@Data
@ToString
@Slf4j
public class Fromcat implements Serializable{
private String transactionHash;
private String transactionIndex;
private String root;
private String blockNumber;
private String blockHash;
private String from;
private String to;
private String gasUsed;
private String contractAddress;
private String[] logs;
private String logsBloom;
private String status;
private String statusMsg;
private String input;
private String output;
private String txProof;
private String receiptProof;
private String message;
private String statusOK;
}
6)前端控制器的编写
@RequestMapping("set")
public Fromcat setNumber() {
String url ="http://{ip}:5002/WeBASE-Front/trans/handle"; //你的webase地址,记得改有注释的地方带星号
String name = "numbers"; //传入你的合约名
String address = "0xf6ec54f02267b6c7fbb19f3f9125b683d777fe1b"; //合约地址 *****
String funcName = "set"; //你合约里的方法 ***** 请确保有solidity 智能合约有set方法
String abi = "[{\"constant\":false,\"inputs\":[{\"name\":\"n\",\"type\":\"string\"}],\"name\":\"set\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"get\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"}]"; //你合约的api
String[] str = {"你好"}; //合约方法中的合约变量设置 *****
WebaseFromcat wf = new WebaseFromcat();
wf.setName(name);
wf.setAddress(address);
wf.setFuncName(funcName);
wf.setAbi(abi);
wf.setFuncParam(str);
String body = webaseUrl.UrlHandle(wf);
String res = HttpUtil.post(url, body);
Fromcat fromcat = JSONObject.parseObject(res, Fromcat.class);
return fromcat;
}
确保编译-部署
浏览器输入http://localhost:5023/set 就可以看到信息
得到刚才设的值
@RequestMapping("get")
public String getNumber() {
String name = "numbers";
String address = "0xf6ec54f02267b6c7fbb19f3f9125b683d777fe1b";
String funcName = "get";
String abi = "[{\"constant\":false,\"inputs\":[{\"name\":\"n\",\"type\":\"string\"}],\"name\":\"set\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"get\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"}]";
WebaseFromcat wf = new WebaseFromcat();
wf.setAbi(abi);
wf.setAddress(address);
wf.setName(name);
wf.setFuncName(funcName);
String body = webaseUrl.UrlHandle(wf);
System.out.println(body);
String res = HttpUtil.post(url, body);
System.out.println(res);
return res;
浏览器输入http://localhost:5023/get就可以看到刚才设的值了