我在前端做了一個ajax post調用。無法將ajax json數據映射到彈簧控制器的對象參數
let data = {
marketplaceId: "1",
monetaryAmount: {
amount: 100,
currencyCode: "USD",
},
}
$.post(posturl, data, function(returnedData) {
console.log(returnedData)
}
).fail(function() {
console.log("error")
})
在我的spring mvc控制器中,我想將monetaryAmount json對象映射到java對象SimpleAmount。
SimpleAmount.java
public class SimpleAmount {
private BigDecimal amount;
private String currencyCode;
public BigDecimal getAmount() {
return this.amount;
}
public void setAmount(BigDecimal amount) {
this.amount = amount;
}
public String getCurrencyCode() {
return this.currencyCode;
}
public void setCurrencyCode(String currencyCode) {
this.currencyCode = currencyCode;
}
}
Controller.java
@RestController
public class Controller {
@RequestMapping(value = "/rpl/updateasinlimit", method = RequestMethod.POST)
@ResponseBody
public BGCServiceResponseStatus updateAsinLimit(
@RequestParam Long marketplaceId,
@RequestParam @RequestBody SimpleAmount monetaryAmount) {
......
}
}
然而,SimpleAmount monetaryAmount
永遠不會被正確映射。在我看來,所有這些原始類型將很容易被映射,而不是Java對象
但我不知道我怎麼能映射從阿賈克斯後發送到傳遞到彈簧控制器參數的JSON對象?