任何人都可以帮助我,我试图将通过ajax发送的复杂json对象转换为对象。这样我就可以使用这个对象进入我的模型。
JSP代码是:
function callRemovefilter()
{
var jsonObjects = {
address1: "Address_1",
city: "City",
pin: "PIN"
};
var jsonObjects2 = {
locality:"Loc1",
shippingType:"Regular",
shippingCost:20
};
var cust= JSON.stringify(jsonObjects);
var sales=JSON.stringify(jsonObjects2);
jQuery.ajax({
url: "http://localhost:8080/OnlineStore/kmsg/grocery/SaveSalesOrder",
type: "GET",
data: {CustomerInfo:cust,SalesModel:sales},
dataType: "json",
beforeSend: function(x) {
if (x && x.overrideMimeType) {
x.overrideMimeType("application/j-son;charset=UTF-8");
}
},
success: function(result) {
//Write your code here
}
});
}
//控制器代码是
@RequestMapping(value = "/SaveSalesOrder", method = RequestMethod.GET)
@ResponseStatus(value=HttpStatus.OK)
public @ResponseBody String SaveCustomerOrder(@RequestParam Map<String,String> requestParams) throws Exception
{
ObjectMapper objectMapper = new ObjectMapper();
SalesCommandObject salesCommandObject= new SalesCommandObject();
salesCommandObject = objectMapper.readValue(body, SalesCommandObject .class);
return "Success";
}
//将对象发送到控制器的JSP代码
var salesCommandObject = {}; salesCommandObject.CustomerInfo =
{
"address1": "Address_1",
"city": "City",
"pin": "PIN"
};
salesCommandObject.SalesModel =
{
"locality":'Loc1',
"shippingType":'Regular',
"shippingCost":20
};
$.ajax
({
type: "POST",
dataType : 'json',
async : true,
url: "http://localhost:8080/OnlineStore/kmsg/grocery/SaveSalesOrder",
data : JSON.stringify(salesCommandObject),
}).done(function(data,type,xml)
{
console.log(data);
}).fail(function()
{
alert("Something Bad Happened, Service failed");
})
答案 0 :(得分:0)
发送对象,而不是jsonstrigs。在你的方法控制器中,SaveCustomerOrder得到一个对象,而不是Map,如:
@RequestMapping(value = "/SaveSalesOrder", method = RequestMethod.GET)
@ResponseStatus(value=HttpStatus.OK)
public @ResponseBody String SaveCustomerOrder(@RequestParam CustomerInfo ci, @RequestParam SalesModel sm) throws Exception
{
并将getter和setter添加到适当的类(即CustomerInfo,SalesModel),如:
//your logic here
return "Success";
}
`public class SalesModel{
private String sale_id;//or whatever field or property you need
public String getSale_Id() {
return sale_id;
}
public void setSale_Id(String si) {
this.sale_id = si;
}
}`