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

Are*_*efe 0 java spring curl spring-mvc

我有一些代码使用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);
}
Run Code Online (Sandbox Code Playgroud)

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

curl -X POST -d "walletName=zyx&currencyName=bitcoin" http://localhost:8080/rest/generateAddress
Run Code Online (Sandbox Code Playgroud)

我想具有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);
Run Code Online (Sandbox Code Playgroud)

我也尝试POST使用JSON数据格式,但我没有改变.这是什么问题?

And*_*fat 6

首先,我建议你从使用System.out.println和使用适当的记录器切换,例如slf4j,因为你可能希望在一个点上将所有输出语句都包含在文件中.

你如何使用spring mvc有些不对劲.由于您已经@RequestPAram为两个字段声明了,为什么不直接使用它们而不是这样做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);
    }
}
Run Code Online (Sandbox Code Playgroud)

测试请求:

curl -X POST -d "walletName=my-cool-wallet&currencyName=ETH" http://localhost:8080/generateAddress
Run Code Online (Sandbox Code Playgroud)

看日志:

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
Run Code Online (Sandbox Code Playgroud)