Java 在SpringMVC中显式使用POST请求值

Java 在SpringMVC中显式使用POST请求值,java,spring,spring-mvc,curl,Java,Spring,Spring Mvc,Curl,我有一些使用Spring MVC项目生成加密货币钱包的代码 @RequestMapping(value = "/generateAddress", method = RequestMethod.POST) public ResponseEntity<WalletInfoWrapper> generateAddress(@RequestParam("walletName") String walletName,

我有一些使用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);
}
我希望按照代码中的规定将
wallet\u Name
currency\u 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
,但没有得到任何更改。这里的问题是什么?

我认为您应该在URL中直接使用查询参数发布帖子,如下所示,因为您正在尝试传递多个参数

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

我认为您应该在URL中直接使用一个查询参数来发布一篇文章,如下所示,因为您正在尝试传递多个参数

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

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

关于如何使用SpringMVC,存在一些不正确的地方。 既然您已经为两个字段声明了
@RequestPAram
s,为什么不直接使用它们而不是执行
request.getPrameter(“blah”)
。因此,我建议删除
@RequestParam
并使用
HttpServletRequest
,反之亦然

我在这里看到的另一件事是
字符串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);
    }
}
查看日志:

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

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

关于如何使用SpringMVC,存在一些不正确的地方。 既然您已经为两个字段声明了
@RequestPAram
s,为什么不直接使用它们而不是执行
request.getPrameter(“blah”)
。因此,我建议删除
@RequestParam
并使用
HttpServletRequest
,反之亦然

我在这里看到的另一件事是
字符串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);
    }
}
查看日志:

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

请给我写下否决投票的理由,我将修改/删除该问题。我有点困惑,不得不再问一次。顺便说一句,我已经删除了前面的问题。你的方法是一篇文章,你的
curl
是一篇文章,但是你说的是
GET
-我很困惑-还要注意你的参数`and作为
@RequestParam(“walletName”)字符串walletName传递,
将参数输入到您的method@ScaryWombat问题确实是关于
POST
,它应该创建带有姓名和地址的钱包。我使用
GET
检查在
POST
之后创建的值。关于参数,我应该注意什么?如果有错误,写下来作为答案。我用GET检查帖子后创建的值——这没有意义。您有变量
walletName
currencyName
-它们已发布给您-故事结束我没有否决请告诉我否决的原因,我将修改/删除该问题。我有点困惑,不得不再问一次。顺便说一句,我已经删除了前面的问题。你的方法是一篇文章,你的
curl
是一篇文章,但是你说的是
GET
-我很困惑-还要注意你的参数`and作为
@RequestParam(“walletName”)字符串walletName传递,
将参数输入到您的method@ScaryWombat问题确实是关于
POST
,它应该创建带有姓名和地址的钱包。我使用
GET
检查在
POST
之后创建的值。关于参数,我应该注意什么?如果有错误,写下来作为答案。我用GET检查帖子后创建的值——这没有意义。你有变量
walletName
currencyName
-它们被发布给你-故事结束我没有下载Votethis不起作用,我假设这不是正确的答案嘿,我下载了代码并执行了,它对我起作用。通过cURL语句以及我的建议,我能够看到值被正确地分解为它们的变量。在本地执行时我注意到的一点是,currencyName字符串currency_name=request.getParameter(“currencyName”)的变量名不正确;字符串currency_Name=request.getParameter(“currencyName”);这不起作用,我认为这不是正确的答案嘿,我把代码拉下来执行了,它对我来说是有效的。通过cURL语句以及我的建议,我能够看到值被正确地分解为它们的变量。在本地执行时我注意到的一点是,currencyName字符串currency_name=request.getParameter(“currencyName”)的变量名不正确;字符串currency_Name=request.getParameter(“currencyName”);