我尝试将JavaScript中的JSON对象发送到Controller。发送对象时,我收到400状态代码。我尝试了很多方法,但我的最后一种方法如下。我错过了什么吗?为什么我一直要400?
ajax post call:
$.ajax({
type: "POST",
url: "http://localhost:8080/services/saveReservation",
cache: false,
data: saveReservation,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data) {
console.debug("After success");
}
});
我的对象看起来像这样:
{
"accommodaion":"a0d8185c-a238-5qe7-aa48-5c196b108aba",
"totalOutputPrice":90,
"bookerInfo":{
"country":"xa",
"homeAddress":"",
"phoneNumber":"0019382663773",
"contactChoice":"phone",
"name":"test name",
"id":"87"
},
"creditCard":{
"holderName":"holder test",
"cardType":"discover",
"cardNumber":"6011303031648258",
"expMonth":"01",
"expYear":"2017",
"cvc":"123"
},
"checkIn":"2016-11-16 06:43:19.77",
"checkOut":"2017-03-16 06:43:19.77",
"totalTax":4,
"totalVat":13,
"roomOutputPrice":"77"
}
控制器:
@RequestMapping(value = "/saveReservation", method = RequestMethod.POST)
public test saveReservation(
@RequestBody test saveReservation) {
System.out.println(saveReservation.getAccommodaion());
System.out.println(saveReservation.getBookerInfo().getName());
System.out.println(saveReservation);
return saveReservation;
}
类:
private class bookerInfo {
private String country;
private String homeAddress;
private String phoneNumber;
private String contactChoice;
private String name;
private String id;
//getters setters
}
private class creditCard {
private String holderName;
private String cardType;
private String expMonth;
private String expYear;
private String cvc;
//getters setters
}
private class test {
private String accommodaion;
private Float totalOutputPrice;
private bookerInfo bookerInfo;
private creditCard creditCard;
private String checkIn;
private String checkOut;
private Float totalTax;
private Float totalVat;
private Float roomOutputPrice;
//getters setters
}
框架错误:
16:12:14,096 DEBUG ExceptionHandlerExceptionResolver:134 - Resolving exception from handler [public ba.projectService.controller.test ba.projectService.controller.G2BController.saveReservation(ba.projectService.controller.test)]: org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Unrecognized field "cardNumber" (class ba.projectService.controller.creditCard), not marked as ignorable (5 known properties: , "cvc", "expMonth", "holderName", "cardType", "expYear"])
at [Source: org.apache.catalina.connector.CoyoteInputStream@793ea01d; line: 1, column: 287] (through reference chain: ba.projectService.controller.test["creditCard"]->ba.projectService.controller.creditCard["cardNumber"]); nested exception is com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "cardNumber" (class ba.projectService.controller.creditCard), not marked as ignorable (5 known properties: , "cvc", "expMonth", "holderName", "cardType", "expYear"])
at [Source: org.apache.catalina.connector.CoyoteInputStream@793ea01d; line: 1, column: 287] (through reference chain: ba.projectService.controller.test["creditCard"]->ba.projectService.controller.creditCard["cardNumber"])
16:12:14,099 DEBUG ResponseStatusExceptionResolver:134 - Resolving exception from handler [public ba.projectService.controller.test ba.projectService.controller.G2BController.saveReservation(ba.projectService.controller.test)]: org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Unrecognized field "cardNumber" (class ba.projectService.controller.creditCard), not marked as ignorable (5 known properties: , "cvc", "expMonth", "holderName", "cardType", "expYear"])
at [Source: org.apache.catalina.connector.CoyoteInputStream@793ea01d; line: 1, column: 287] (through reference chain: ba.projectService.controller.test["creditCard"]->ba.projectService.controller.creditCard["cardNumber"]); nested exception is com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "cardNumber" (class ba.projectService.controller.creditCard), not marked as ignorable (5 known properties: , "cvc", "expMonth", "holderName", "cardType", "expYear"])
at [Source: org.apache.catalina.connector.CoyoteInputStream@793ea01d; line: 1, column: 287] (through reference chain: ba.projectService.controller.test["creditCard"]->ba.projectService.controller.creditCard["cardNumber"])
16:12:14,099 DEBUG DefaultHandlerExceptionResolver:134 - Resolving exception from handler [public ba.projectService.controller.test ba.projectService.controller.G2BController.saveReservation(ba.projectService.controller.test)]: org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Unrecognized field "cardNumber" (class ba.projectService.controller.creditCard), not marked as ignorable (5 known properties: , "cvc", "expMonth", "holderName", "cardType", "expYear"])
at [Source: org.apache.catalina.connector.CoyoteInputStream@793ea01d; line: 1, column: 287] (through reference chain: ba.projectService.controller.test["creditCard"]->ba.projectService.controller.creditCard["cardNumber"]); nested exception is com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "cardNumber" (class ba.projectService.controller.creditCard), not marked as ignorable (5 known properties: , "cvc", "expMonth", "holderName", "cardType", "expYear"])
at [Source: org.apache.catalina.connector.CoyoteInputStream@793ea01d; line: 1, column: 287] (through reference chain: ba.projectService.controller.test["creditCard"]->ba.projectService.controller.creditCard["cardNumber"])
答案 0 :(得分:2)
您需要使用JSON.stringify序列化您的javascript对象。
尝试:
$.ajax({
type: "POST",
url: "http://localhost:8080/services/saveReservation",
cache: false,
data: JSON.stringify(saveReservation),
contentType: "application/json; charset=utf-8",
dataType: "json",
processData: false,
success: function(data) {
console.debug("After success");
}
});
并将内容类型添加到请求映射:
@RequestMapping(value = "/saveReservation", method = RequestMethod.POST, consumes=MediaType.APPLICATION_JSON_VALUE)
确保已定义json消息转换器。如果你有杰克逊作为家属,春天为你做。
您的新消息很明确。您正在从您的班级中不存在的javascript发送属性。我不确定你是犯了错误还是你想要的。该物业" cardNumber"在您的CreditCard bean中不存在。
您可以将Jackson配置为忽略未知属性,而不会使用类顶部的注释@JsonIgnoreProperties
抛出异常。
@JsonIgnoreProperties(ignoreUnknown = true)
private class creditCard {
private String holderName;
private String cardType;
private String expMonth;
private String expYear;
private String cvc;
//getters setters
}
如果您想发送多张信用卡,可以使用清单。因此,只需更改测试bean即可使用卡列表
private class test {
...
private List<creditCard> creditCards;
...
//getters setters
}
在您的javascript对象中,它现在应该是一个数组
{
... here all your properties
"creditCards":[{
"holderName":"card 1",
"cardType":"discover",
"cardNumber":"6011303031648258",
"expMonth":"01",
"expYear":"2017",
"cvc":"123"
},{
"holderName":"card 2",
"cardType":"discover",
"cardNumber":"6011303031648258",
"expMonth":"01",
"expYear":"2017",
"cvc":"123"
},{
"holderName":"card 3",
"cardType":"discover",
"cardNumber":"6011303031648258",
"expMonth":"01",
"expYear":"2017",
"cvc":"123"
}],
... more properties
}
应该足够了。我希望你明白这一点。