我试图创建一个api,在其中我必须在json对象中提供事务日志,但是im无法将replayPastTransactionsFlowable的结果添加到集合对象中
@GetMapping("/test")
public List<Transaction> test() throws InterruptedException, ExecutionException, IOException {
String address = "0xeedc155ae507fb0a9becae6315286183a23229b0";
List<Transaction> transactions = new ArrayList<Transaction>();
Web3j web3j = web3jConfig.initializeWeb3j();
System.out.println("test Running.......");
BigInteger block = web3j.ethBlockNumber().send().getBlockNumber();
System.out.println("blockno:" + block.intValue());
int block_no = block.intValue();
Flowable<Transaction> replyBlocks = web3j.replayPastTransactionsFlowable(DefaultBlockParameterName.EARLIEST, DefaultBlockParameterName.LATEST);
replyBlocks.forEach(temp->{
System.out.println("into replyBlocks");
System.out.println(temp.getBlockNumber());
transactions.add(temp); //It's an collection object,and value is not adding into it
}) ;
return transactions;
}
}我使用以下两个dep
<dependency>
<groupId>org.web3j</groupId>
<artifactId>core</artifactId>
<version>4.5.5</version>
</dependency>
<dependency>
<groupId>org.web3j</groupId>
<artifactId>contracts</artifactId>
<version>4.5.5</version>
</dependency>
发布于 2020-02-14 16:50:48
尝尝这个
import java.math.BigInteger;
import org.springframework.web.bind.annotation.*;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.methods.response.Transaction;
import org.web3j.protocol.core.methods.response.Web3ClientVersion;
import org.web3j.protocol.http.HttpService;
import io.reactivex.Flowable;
@RestController
@RequestMapping("/api")
public class SimpleResource {
@GetMapping("/transactions")
public Flowable<Transaction> getTransactions() throws Exception {
System.out.println("call transactions");
Web3j web3j = Web3j.build(new HttpService());
Web3ClientVersion web3ClientVersion = web3j.web3ClientVersion().send();
String clientVersion = web3ClientVersion.getWeb3ClientVersion();
System.out.println("clientVersion......." + clientVersion);
BigInteger block = web3j.ethBlockNumber().send().getBlockNumber();
System.out.println("blockno:" + block.intValue());
Flowable<Transaction> transactions = web3j.replayPastTransactionsFlowable(DefaultBlockParameterName.EARLIEST,
DefaultBlockParameterName.LATEST).serialize();
return transactions;
}
}
https://ethereum.stackexchange.com/questions/79857
复制相似问题