我正在使用带有效负载的Postman发布POST:
{
"id" : "6",
"type" : "BUY",
"numOfShares" : "5000",
"pricePerShare" : "45.23",
"ticker" : "XYZ",
"commission" : "4.95",
"comments" : "Mgmt Change"
}
在REST控制器中处理我的POST的逻辑是:
@PostMapping("/transactions")
public Transaction addTransaction(@PathVariable Map<String, String> newTransaction){
int transID = Integer.parseInt(newTransaction.get("id"));
TransactionType transType;
if(newTransaction.get("type").toUpperCase().equals("BUY")){
transType = TransactionType.BUY;
}else if(newTransaction.get("type").toUpperCase().equals("SELL")){
transType = TransactionType.SELL;
}else{
transType = null;
}
...MORE LOGIC HERE...
return transMD.recordNewTransaction(new Transaction(transID,transType,transNumOfShares,
transPricePerShare,transTickerSymbol,transCommission,transComments));
}
当我发布POST时,我得到&#34; java.lang.NumberFormatException:null&#34;在int transID = Integer.parseInt(newTransaction.get("id"));
当我在调试模式下运行应用程序并执行POST时,我可以看到当达到addTransaction()
方法时,newTransaction
映射为空(因此上面出现Null错误)。
为什么地图不会被填充?
编辑:我明确添加到地图的单元测试工作:
@Test
public void makeNewTransaction() throws Exception{
passedTransaction.put("id", "6");
passedTransaction.put("type", "BUY");
passedTransaction.put("numOfShares", "300");
passedTransaction.put("pricePerShare", "37.23");
passedTransaction.put("ticker", "XIV");
passedTransaction.put("commission", "4.95");
passedTransaction.put("comments","Testing Testing 1 2 3");
Transaction newTrans = restCtrl.addTransaction(passedTransaction);
assertEquals("XIV", newTrans.getSymbol());
}