我编写了一些Java钱包生成代码,并将其用于生成加密货币钱包。该代码提供,POST请求后返回空值
public synchronized WalletInfo generateAddress(GenerateWallet generateWallet) {
final WalletInfo walletInfo = new WalletInfo();
String walletName = generateWallet.getWalletName();
String currencyName = generateWallet.getCurrencyName();
WalletInfo walletInfoDb = iWalletInfoDao.getWalletInfoWithWalletNameAndCurrency(walletName, currencyName);
if (walletInfoDb == null && genWalletMap.get(walletName) == null) {
String currency = currencyName.toUpperCase();
if (currency.equals("BITCOIN")) {
final WalletManager walletManager = WalletManager.setupWallet(walletName);
walletManager.addWalletSetupCompletedListener((wallet) -> {
Address address = wallet.currentReceiveAddress();
WalletInfo newWallet = createWalletInfo(walletName, currencyName, address.toString());
// set the properties of the walletInfo
// the instance is final and can work inside the lambda expression
walletInfo.setId(newWallet.getId());
walletInfo.setName(newWallet.getName());
walletInfo.setAddress(newWallet.getAddress());
walletInfo.setCurrency(newWallet.getCurrency());
walletMangersMap.put(newWallet.getId(), walletManager);
genWalletMap.remove(walletName);
});
genWalletMap.put(walletName, walletManager);
return walletInfo;
} else if (currency.equals("ETHEREUM")) {
return walletInfo;
} else {
return walletInfo;
}
}
return walletInfo;
}
当我使用cURL
,
curl -H "Content-Type: application/json" -X POST -d '{"walletName": "Florence8","currencyName":"Bitcoin"}' http://localhost:8080/rest/wallet/generateAddress
我得到null
是返回一个POST
要求,
{
"id" : null,
"name" : null,
"address" : null,
"currency" : null
}
虽然产生的实体,仍然坚持在MySQL中。
我继续调试,这是有线。调试不遵循代码的top-to-bottom
序列。调试的顺序是什么样子,
我的意思是,如果代码涉及到这一行walletManager.addWalletSetupCompletedListener((wallet)
那么它就应该执行里面的操作吧?
任何操作如何在数据库中正确保存后返回实体?在lambda表达式中使用
请将您的代码缩减为[mcve]。 –
为什么不直接制作'walletInfo'' final'? –
我没有看到上面那个'walletInfo'不能是最终的任何理由。删除'= new WalletInfo()',因为您只是将其覆盖在下一行上,并将其合并到一行'final WalletInfo walletInfo = iWalletInfoDao/* ...* /;'还要注意,在if(walletInfo == null)分支中调用'walletInfo'上的方法没有什么意义,因为这样做会抛出一个NPE。 –