在POST请求之后返回java Null
我编写了一些Java钱包生成代码,并使用它生成加密货币钱包。提供了代码
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
执行POST
请求时
curl -H "Content-Type: application/json" -X POST -d '{"walletName": "Florence8","currencyName":"Bitcoin"}' http://localhost:8080/rest/wallet/generateAddress
我得到了回报
{
"id" : null,
"name" : null,
"address" : null,
"currency" : null
}
而实体是生成的,并且仍然保存在MySQL中
我一直在调试,这是有线的。调试不遵循代码的top-to-bottom
序列。调试的顺序如下:
我的意思是,如果代码到达这一行walletManager.addWalletSetupCompletedListener((wallet)
,那么它应该执行内部的操作,对吗
有什么建议吗?在数据库中正确保存实体后,我如何取回实体
# 1 楼答案
问题是,在声明变量后重新分配值——事实上,第一个赋值是多余的,因为您只需覆盖值而不使用它
所以-通过删除第一个赋值使其有效
final
:或者实际上是:
此外,这种情况:
是反向的:在该块中,调用
walletInfo
上的方法;调用将以NullPointerException
失败,因为walletInfo
为空