我編寫了一些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。 –