2017-03-28 25 views
0

JSONオブジェクトをJavaScriptからControllerに送信しようとしました。私は、オブジェクトを送信するときに400のステータスコードを取得します。私は多くの方法を試しましたが、私の最後のアプローチは以下の通りです。私は何かが恋しいですか?なぜ私は400を得続けますか?JSONをコントローラに送信するとエラーが発生する

Ajaxのポストコール:

$.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: [email protected]; 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: [email protected]; 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: [email protected]; 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: [email protected]; 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: [email protected]; 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: [email protected]; line: 1, column: 287] (through reference chain: ba.projectService.controller.test["creditCard"]->ba.projectService.controller.creditCard["cardNumber"]) 
+0

エラーのログを投稿できますか? – Karim

+0

@RequestMapping(method = RequestMethod.POST、consumes = {MediaType.APPLICATION_JSON_VALUE}) –

+0

私はエラーを投函しました。 @SannonAragão私は消費セクションを追加しましたが、私はまだ400を取得 –

答えて

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

と要求mappgingにコンテンツタイプを追加します。

@RequestMapping(value = "/saveReservation", method = RequestMethod.POST, consumes=MediaType.APPLICATION_JSON_VALUE) 

あなたは、JSONメッセージコンバータを定義していることを確認します。もしあなたがJacksonを依存している春にしたら、あなたのためにそれをしてください。


新しいメッセージはクリアです。クラスに存在しないjavascriptからプロパティを送信しています。あなたが間違いを犯したのか、それともあなたが望むものなのか分かりません。プロパティ "cardNumber"は、あなたのCreditCard Beanには存在しません。

クラスの先頭に注釈@JsonIgnorePropertiesを使用して例外をスローせずに、未知のプロパティを無視するようにJacksonを設定できます。

@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 
} 

でなければなりません。私はあなたがそのアイデアを得ることを望みます。

+0

私はあなたの答えに従いますが、まだ400を取得します。 私は私のXMLにジャクソンを持っています。 <ビーンID = "jsonMessageConverter" \t \tクラス= "org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> \t。 また、私はフレームワークに表示されている新しいエラーを追加しました –

+0

@MalenaT私は自分の答えを編集しました。 – alfcope

+0

私はそれを逃したにちがいない...今私は価値を得ることができる。どうもありがとうございます。 また、jsonに2つのcreditCardオブジェクトがあるとします。 forループを作成するか、これら2つのクレジットカード情報を入手するにはどうすればよいですか? –

関連する問題