2017-08-14 57 views
0

我有一些代码可以使用Spring MVC项目生成加密货币钱包。明确使用Spring MVC中的POST请求值

@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); 

我也试图POST使用数据的JSON格式,我没有得到任何改变。这里有什么问题?

+0

请写下我downvote的原因,我将修改/删除的问题。我有些困惑,不得不再次提问。顺便说一句,我已经删除了上一个问题。 – Arefe

+1

你的方法是一个POST,你的'curl'是一个POST,但你说的是'GET' - 我很困惑 - 还要注意你的参数并且作为@RequestParam(“walletName”)传递了字符串walletName,''参数到你的方法 –

+0

@ScaryWombat这个问题的确是关于'POST',它应该创建名称和地址的钱包。我使用'GET'来检查'POST'后创建的值。我应该注意哪些参数?如果出现错误,请将其写为答案。 – Arefe

回答

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); 
    } 
} 

检验出要求:

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,就像下面这个URL一样,因为你试图传递多个参数。

所有的
curl -X POST http://localhost:8080/rest/generateAddress?walletName=zyx&currencyName=bitcoin 
+0

这不起作用,我认为这不是正确的答案 – Arefe

+0

嘿,我拉下了代码并执行,它对我有用。 我能够将你的cURL语句以及我所建议的值正确地分解到它们的变量中。 我在本地执行时注意到的一件事是,您的货币名称有一个错误的变量名称 String currency_Name = request.getParameter(“currency ** n ** ame”); String currency_Name = request.getParameter(“currency ** N ** ame”); –