Java 使用Jackson在@RequestBody上发布400个错误请求
在SpringMVC控制器中将JSON映射到Java对象时,我收到了400个错误的Ajax请求。我已经检查了主题中的大部分相关问题,但仍然无法使其正常工作 Ajax调用和JSON:Java 使用Jackson在@RequestBody上发布400个错误请求,java,json,ajax,spring-mvc,jackson,Java,Json,Ajax,Spring Mvc,Jackson,在SpringMVC控制器中将JSON映射到Java对象时,我收到了400个错误的Ajax请求。我已经检查了主题中的大部分相关问题,但仍然无法使其正常工作 Ajax调用和JSON: $.ajax({ type: "POST", url: "vocabulary/createVocabulary", contentType : 'application/json; charset=utf-8', data: {"vocabularyName" : "a",
$.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/词汇表/CreateVocability时出现问题。原因:
错误的请求
此外,如果从控制器中删除“@RequestBody”注释,则会得到以下服务器响应:
未能实例化[com.attila.词汇表.ux.spring.词汇表.DTO.VocabularyDTO]:未找到默认构造函数;嵌套异常是java.lang.NoSuchMethodException:com.attila.词汇表.ux.spring.词汇表.DTO.VocabularyDTO.()
有人知道会出什么问题吗?谢谢
更新:
没有'@RequestBody'注释的选项现在实际上正在工作(我犯了一个愚蠢的错误,为什么以前没有),也就是说,没有抛出错误,但是没有将值传递给对象
我希望它也能修复注释,但我仍然得到了400错误。使用
@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";
}
}
这400条消息中没有其他错误消息?据我所知,这是我在浏览器中得到的全部响应。有没有办法了解更多的信息?谢谢!序列化有帮助。它现在工作正常了。