Java POST请求保留在MySQL中,但返回null
我编写了一些Java钱包生成代码,并使用它生成加密货币钱包。提供了代码Java POST请求保留在MySQL中,但返回null,java,Java,我编写了一些Java钱包生成代码,并使用它生成加密货币钱包。提供了代码 public synchronized WalletInfo generateAddress(GenerateWallet generateWallet) { final WalletInfo walletInfo = new WalletInfo(); String walletName = generateWallet.getWalletName(); String
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
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
我得到null是返回值
{
"id" : null,
"name" : null,
"address" : null,
"currency" : null
}
而实体是生成的,并且仍然保存在MySQL中。我一直在调试,这是有线的。调试不遵循代码从上到下的顺序。调试的顺序如下:
我想指出的一点是,如果代码到达这一行
walletManager.addWalletSetupCompletedListener((wallet)
,然后它应该执行内部的操作
在数据库中正确保存实体后,我如何取回实体?如果需要,我可以提供更多信息
更新
正如答案中所建议的,我使用了一个倒计时闩锁
,解决了这个问题
public synchronized WalletInfo generateAddress(GenerateWallet generateWallet) {
CountDownLatch finshedSetup = new CountDownLatch(1);
// some code
}
终端中的输出
看起来您遇到了竞争条件。您正在设置一个回调以填充
walletInfo
,但可能在回调执行之前返回walletInfo
返回前等待的某种类型的承诺或闩锁可能会有所帮助。例如:
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);
CountDownLatch finishedSetup = new CountDownLatch(1);
walletManager.addWalletSetupCompletedListener((wallet) -> {
Address address = wallet.currentReceiveAddress();
WalletInfo newWallet = createWalletInfo(walletName, currencyName, address.toString());
// set the properties of the walletInfo
walletInfo.setId(newWallet.getId());
walletInfo.setName(newWallet.getName());
walletInfo.setAddress(newWallet.getAddress());
walletInfo.setCurrency(newWallet.getCurrency());
walletMangersMap.put(newWallet.getId(), walletManager);
genWalletMap.remove(walletName);
finshedSetup.countDown();
});
genWalletMap.put(walletName, walletManager);
finishedSetup.await();
return walletInfo;
} else if (currency.equals("ETHEREUM")) {
return walletInfo;
} else {
return walletInfo;
}
}
return walletInfo;
}
我对多线程的知识有限。你能帮我解决吗?我可以提供所需代码的其他部分。@Artin用倒计时锁存器示例进行了更新。更多关于锁存器如何工作的阅读:感谢你的回答和更新。代码中的
finishedSetup
方法是什么?你能提供一个示例吗?我会的一旦我了解了发生的事情,就立即接受答案。finishedSetup
是我用count 1实例化的一个新的倒计时锁存器。wait()
等待倒计时达到0。countdown()
将计数递减1。