使用Jackson的@RequestBody在ajax帖子上发出400个错误请求

时间:2017-03-04 18:01:51

标签: java json ajax spring-mvc jackson

在尝试将我的JSON映射到spring MVC控制器中的Java对象时,我收到了关于Ajax请求的400 Bad Request。我已经检查了主题中的大部分相关问题,但仍无法使其正常工作

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"

...
}

我得到的错误:

  

HTTP错误400   访问/ ux /词汇/ createVocabulary的问题。原因:   BAD_REQUEST

此外,如果我从我的控制器中删除'@RequestBodyannotation,我会得到以下服务器响应:

  

无法实例化[com.attila.vocabulary.ux.spring.vocabulary.DTO.VocabularyDTO]:找不到默认构造函数;嵌套异常是java.lang.NoSuchMethodException:com.attila.vocabulary.ux.spring.vocabulary.DTO.VocabularyDTO。()

有谁知道可能出现什么问题?谢谢!

更新: 没有'@RequestBody'注释的选项现在实际上正在工作(这是我之前为什么没有的愚蠢错误) - 即不抛出错误,但是没有将值传递给对象。

我希望它能修复注释,但我仍然会得到400错误。

1 个答案:

答案 0 :(得分:1)

使用@RequestBody时代码无效,因为您的ajax请求未发送json对象。您需要在发送之前序列化JavaScript对象,使用JSON.stringify()。为什么你在控制器上使用@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";
  }
}