【问题标题】:Use explicitly the POST request values in Spring MVC在 Spring MVC 中显式使用 POST 请求值
【发布时间】:2017-08-14 05:34:43
【问题描述】:

我有一些代码可以使用 Spring MVC 项目生成加密货币钱包。

@RequestMapping(value = "/generateAddress", method = RequestMethod.POST)
public ResponseEntity<WalletInfoWrapper> generateAddress(@RequestParam("walletName") String walletName,
                                                         @RequestParam("currencyName") String currencyName, HttpServletRequest request) {

    String wallet_Name = request.getParameter("walletName");
    String currency_Name = request.getParameter("currencyname");

    System.out.println("wallet_Name = " + wallet_Name + " , currency_Name = " + currency_Name);

    // return if the wallet name or the currency is null
    if (Objects.isNull(wallet_Name) || Objects.isNull(currency_Name)) {
        return new ResponseEntity<WalletInfoWrapper>(HttpStatus.NOT_ACCEPTABLE);
    }

    WalletInfo walletInfo = walletService.generateAddress(wallet_Name);

    if (Objects.isNull(walletInfo)) {
        return new ResponseEntity<WalletInfoWrapper>(HttpStatus.NOT_ACCEPTABLE);
    }

    WalletInfoWrapper walletInfoWrapper = new WalletInfoWrapper();
    walletInfoWrapper.setName(walletInfo.getName());

    return new ResponseEntity<WalletInfoWrapper>(walletInfoWrapper, HttpStatus.CREATED);
}

现在,我提出这个POST 请求,

curl -X POST -d "walletName=zyx&currencyName=bitcoin" http://localhost:8080/rest/generateAddress

我希望将wallet_Namecurrency_Name 分开并按照代码中提供的方式打印。但是,在我发出POST 请求后,我在控制台中什么也看不到。

        String wallet_Name = request.getParameter("walletName");
        String currency_Name = request.getParameter("currencyname");

        System.out.println("wallet_Name = " + wallet_Name + " , currency_Name = " + currency_Name);

我还尝试使用JSON 格式的数据POST,但没有任何变化。这里有什么问题?

【问题讨论】:

  • 请写下投反对票的理由,我将修改/删除问题。我有点困惑,只好再问一遍。顺便说一句,我有deleted 上一个问题。
  • 您的方法是用于 POST,您的 curl 是 POST,但您在谈论 GET - 我很困惑 - 另请注意您的参数`并作为 @RequestParam("walletName") String walletName, 参数传递给你的方法
  • @ScaryWombat 这个问题确实是关于POST,它应该创建带有名称和地址的钱包。我使用GET 检查在POST 之后创建的值。关于参数我应该注意什么?如果有错误,请写下答案。
  • 我使用 GET 来检查在 POST 之后创建的值 - 这没有意义。您有变量 walletNamecurrencyName - 它们已发布给您 - 故事结束
  • 我没有投反对票

标签: java spring spring-mvc curl


【解决方案1】:

首先,我建议您不要使用System.out.println,而是使用适当的记录器,例如slf4j,因为您可能希望在某一时刻将所有输出语句保存到文件中。

你使用 spring mvc 的方式有些不对劲。 既然您已经为您的两个字段声明了@RequestPArams,为什么不直接使用它们而不是使用request.getPrameter("blah")。所以我建议要么删除@RequestParam,然后使用HttpServletRequest,反之亦然。

我在这里看到的另一件事String currency_Name = request.getParameter("currencyname"); 你犯了一个错误。您应该使用request.getParameter("currencyName")(注意 N 大写)。

这是我为您的请求提供的示例。我添加了使用@RequestParam 和使用request.getParameter()。你想用什么是你的选择。现在的建议是使用@RequestParam

@RestController
public class WalletController {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @RequestMapping(value = "/generateAddress", method = RequestMethod.POST)
    public ResponseEntity<String> generateAddress(@RequestParam("walletName") String walletName,
                                                  @RequestParam("currencyName") String currencyName, HttpServletRequest request) {
        logger.info("walletName {} and currencyName {}", walletName, currencyName);
        String wallet_Name = request.getParameter("walletName");
        String currency_Name = request.getParameter("currencyName");
        logger.info("walletName {} and currencyName {}", wallet_Name, currency_Name);

        // DO other Stuff
        return ResponseEntity.ok(null);
    }
}

测试请求:

curl -X POST -d "walletName=my-cool-wallet&currencyName=ETH" http://localhost:8080/generateAddress

查看日志:

2017-08-14 10:11:29.972  INFO 29228 --- [nio-8080-exec-1] c.s.s.controller.WalletController        : walletName my-cool-wallet and currencyName ETH
2017-08-14 10:11:29.972  INFO 29228 --- [nio-8080-exec-1] c.s.s.controller.WalletController        : walletName my-cool-wallet and currencyName ETH

【讨论】:

    【解决方案2】:

    我认为您应该直接在 URL 中使用查询参数进行 POST,如下所示,因为您尝试传递多个参数。

    curl -X POST http://localhost:8080/rest/generateAddress?walletName=zyx&currencyName=bitcoin
    

    【讨论】:

    • 这不起作用,我认为这不是正确的答案
    • 嘿,我拉下代码并执行,它对我有用。我可以使用您的 cURL 语句以及我的建议正确地将值拆分为它们的变量。我在本地执行时注意到的一件事是您的 currencyName String currency_Name = request.getParameter("currencyname"); 的变量名称错误。 String currency_Name = request.getParameter("currencyName");
    猜你喜欢
    • 2016-11-28
    • 1970-01-01
    • 1970-01-01
    • 2018-08-06
    • 1970-01-01
    • 1970-01-01
    • 2015-09-19
    • 1970-01-01
    • 2015-05-28
    相关资源
    最近更新 更多