2017-03-04 3 views
0

Spring MVCコントローラのJavaオブジェクトにJSONをマップしようとすると、Ajaxリクエストで400の不正リクエストが発生します。私は、トピックに関連する質問のほとんどを確認しているが、まだそれはジャクソンを使用して@RequestBodyでAjax投稿に400のリクエストがありません

Ajax呼び出しとJSONを動作させることができませんでした:

$.ajax({ 
    type: "POST", 
    url: "vocabulary/createVocabulary", 
    contentType : 'application/json; charset=utf-8', 
    data: {"vocabularyName" : "a", 
    "vocabularyDescription" : "b"}, 

マイコントローラ:

@Service 
@Controller 
@RequestMapping(path="vocabulary") 
public class VocabularyController { 

@RequestMapping (path = "createVocabulary", method = RequestMethod.POST) 
@ResponseBody String createVocabulary(@RequestBody VocabularyDTO vocabularyDTO){ 
    return "Success"; 
    } 
} 

私のJavaオブジェクト:

public class VocabularyDTO { 

private String vocabularyName; 
private String vocabularyDescription; 

public VocabularyDTO(){}; 

public String getVocabularyName() { 
    return vocabularyName; 
} 

public void setVocabularyName(String vocabularyName) { 
    this.vocabularyName = vocabularyName; 
} 

public String getVocabularyDescription() { 
    return vocabularyDescription; 
} 

public void setVocabularyDescription(String vocabularyDescription) { 
    this.vocabularyDescription = vocabularyDescription; 
} 
} 

私はSpring 4.2.5とJacksonを使用します:

... 

def springVersion = "4.2.5.RELEASE" 

repositories { 
mavenCentral() 
jcenter() 
} 

dependencies { 

compile group: "org.springframework", name: "spring-core", version: "$springVersion" 
compile group: "org.springframework", name: "spring-beans", version: "$springVersion" 
compile group: "org.springframework", name: "spring-context", version: "$springVersion" 
compile group: "org.springframework", name: "spring-aop", version: "$springVersion" 
compile group: "org.springframework", name: "spring-web", version: "$springVersion" 
compile group: "org.springframework", name: "spring-webmvc", version: "$springVersion" 
compile group: "org.springframework.data", name: "spring-data-jpa", version: "1.9.4.RELEASE" 

compile group: "com.fasterxml.jackson.core", name: "jackson-databind", version: "2.6.5" 
compile group: "com.fasterxml.jackson.core", name: "jackson-core", version: "2.6.5" 
compile group: "com.fasterxml.jackson.core", name: "jackson-annotations", version: "2.6.5" 

... 
} 

私は取得していますエラー:/ UX /語彙/ createVocabularyにアクセス

HTTP ERROR 400 問題。理由: BAD_REQUEST

私は私のコントローラから」@のRequestBody'annotationを削除する場合はさらに、私は、次のサーバーの応答を取得する:

はインスタンス化に失敗しました[com.attila.vocabulary.ux.spring .vocabulary.DTO.VocabularyDTO]:デフォルトコンストラクタが見つかりませんでした。 。ネストされた例外はjava.lang.NoSuchMethodExceptionです:com.attila.vocabulary.ux.spring.vocabulary.DTO.VocabularyDTO()

誰が間違っている可能性がどのような任意のアイデアを持っていますか?ありがとう!

UPDATE: '@RequestBody'アノテーションのないオプションは、実際には現在動作しています(これは以前の私の愚かなミスです)。つまり、値をオブジェクトに渡すことなくエラーをスローしません。

私はアノテーションも修正することを望んでいましたが、私はまだそのような方法で400エラーを取得します。

+0

その400には他にエラーメッセージはありませんか? – Jeremy

+0

私が知っていることではありません - これは私がブラウザで回答として得たものです。それについてもっと知る方法はありますか? – ADR

答えて

0

あなたのajaxリクエストがjsonオブジェクトを送信していないため、@RequestBodyを使用しているときにコードが機能しません。 JSON.stringify()を使用してJavaScriptオブジェクトを送信する前に、シリアル化する必要があります。あなたのコントローラで@Serviceを使っているのはなぜですか?試してみてください:

$.ajax({ 
    type: "POST", 
    url: "vocabulary/createVocabulary", 
    contentType : 'application/json; charset=utf-8', 
    data: JSON.stringify({"vocabularyName" : "a", 
    "vocabularyDescription" : "b"}), 


@Controller 
@RequestMapping(path="vocabulary") 
public class VocabularyController { 

@RequestMapping (path = "createVocabulary", method = RequestMethod.POST, consumes=MediaType.APPLICATION_JSON_VALUE} 
@ResponseBody String createVocabulary(@RequestBody VocabularyDTO vocabularyDTO){ 
    return "Success"; 
    } 
} 
+0

ありがとうございました!シリアライゼーションは助けになりました。今は正しく動作しています。 – ADR

関連する問題