有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在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序列。调试的顺序如下:

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

我的意思是,如果代码到达这一行walletManager.addWalletSetupCompletedListener((wallet),那么它应该执行内部的操作,对吗

有什么建议吗?在数据库中正确保存实体后,我如何取回实体


共 (1) 个答案

  1. # 1 楼答案

    Variable used in the lambda expression should be final or effectively final

    问题是,在声明变量后重新分配值——事实上,第一个赋值是多余的,因为您只需覆盖值而不使用它

    所以-通过删除第一个赋值使其有效final

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

    或者实际上是:

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

    此外,这种情况:

    if (walletInfo == null) {
    

    是反向的:在该块中,调用walletInfo上的方法;调用将以NullPointerException失败,因为walletInfo为空