POST请求后返回Null

时间:2017-08-31 09:46:06

标签: java

我写了一些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;
    }

当我使用POST

进行cURL请求时
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中。

我一直在调试,这是有线的。调试不遵循代码的top-to-bottom顺序。调试的顺序就像,

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

我的意思是如果代码来到这一行walletManager.addWalletSetupCompletedListener((wallet)那么它应该在里面执行操作,对吧?

任何想法如何在合理地持久保存在数据库中后恢复该实体?

1 个答案:

答案 0 :(得分:2)

  

lambda表达式中使用的变量应该是final或者有效的

问题是您在声明变量后重新分配值 - 实际上,第一个赋值是多余的,因为您只是在不使用它的情况下覆盖该值。

所以 - 通过删除第一个作业来有效地final

WalletInfo walletInfo = iWalletInfoDao.getWalletInfoWithWalletNameAndCurrency(walletName, currencyName);

实际上 final

final WalletInfo walletInfo = iWalletInfoDao.get/*etc*/

此外,这个条件:

if (walletInfo == null) {

被反转:在该块内,您调用walletInfo上的方法;调用将因NullPointerException而失败,因为walletInfo为空。