在Ajax请求中,当我尝试将Spring JVM映射到Java对象时,我得到了400错误的请求。我检查了大部分的主题中的相关问题,但仍然不能使它工作400使用Jackson的@RequestBody在ajax帖子上发出错误请求
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
此外,如果我从我的控制器中删除“@ RequestBody'annotation我得到以下服务器响应:
无法实例[com.attila.vocabulary.ux.spring .vocabulary.DTO.VocabularyDTO]:找不到默认构造函数;嵌套的例外是java.lang.NoSuchMethodException:com.attila.vocabulary.ux.spring.vocabulary.DTO.VocabularyDTO()
没有人有任何想法可能是错误的?谢谢!
UPDATE: 没有“@RequestBody”注释现在实际工作(这是我的一个愚蠢的错误,为什么之前没有)选项 - 即不扔的错误,但是没有被传递给对象的值。
我希望它能修复注释,但我仍然得到这样的400错误。
有没有其他的错误信息,以及400? – Jeremy
不是我所知道的 - 这是我在浏览器中得到的所有答案。有什么方法可以找到更多关于它的信息吗? – ADR