尝试在Spring MVC控制器中将JSON映射到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 / vocabulary / createVocabulary时出现问题。原因:
错误的请求
此外,如果我从控制器中删除“ @RequestBody”注释,则会收到以下服务器响应:
无法实例化[com.attila.vocabulary.ux.spring.vocabulary.DTO.VocabularyDTO]:未找到默认构造函数;嵌套的异常是java.lang.NoSuchMethodException:com.attila.vocabulary.ux.spring.vocabulary.DTO.VocabularyDTO。()
有人知道什么地方可能出问题吗?谢谢!
更新:
没有'@RequestBody'注释的选项现在实际上正在工作(这是我的一个愚蠢的错误,为什么以前没有这样做)-即不引发错误,但是没有将值传递给对象。
我希望它也可以修复注释,但是我还是会收到400错误。
最佳答案
使用@RequestBody
时,您的代码无法正常工作,因为您的ajax请求未发送json对象。您需要使用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";
}
}