java - 在 Spring MVC 中显式使用 POST 请求值

标签 java spring spring-mvc curl

我有一些使用 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 ,但没有得到任何改变。这里有什么问题吗?

最佳答案

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

您使用 spring mvc 的方式有一些不正确的地方。 既然你已经有了@RequestPAram为您的两个字段声明了 s,为什么不直接使用它们而不是 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

关于java - 在 Spring MVC 中显式使用 POST 请求值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45667977/

相关文章:

java - 如何在log4j中自动折叠重复的日志输出

java - Spring MVC + hibernate

java - Xamarin.Forms WebView 无法与 WebRTC 配合使用

java - 如何防止 RadioGroup 事件?

使用 eclipse 或 STS 的 Spring Batch

java - 如何正确查询多个嵌套数组中的字符串匹配项? - MongoDB

java - Swing 应用程序中的 Hibernate 和 Spring

java - Spring MVC 的错误处理

Java Web 应用程序 Hello World

java - 如何在 hql/hibernate 中的值集中使用 equals?