当前位置: 首页 > 知识库问答 >
问题:

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

乜建柏
2023-03-14

当试图将我的JSON映射到Spring MVC控制器中的Java对象时,我在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"

...
}

我遇到的错误:

HTTP错误400访问/ux/词汇表/createVocationary时出现问题。原因:糟糕的请求

此外,如果我从控制器中删除“@RequestBody”注释,我会得到以下服务器响应:

未能实例化[com.attila.词汇表.ux.spring.词汇表.DTO.VocabularyDTO]:未找到默认构造函数;嵌套的异常是java。lang.NoSuchMethodException:com。阿提拉。词汇用户体验。Spring词汇DTO。词汇DTO。()

有人知道会出什么问题吗?谢谢

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

我希望它也能修复注释,但这样我仍然得到了400个错误。

共有1个答案

颜功
2023-03-14

使用@Request estbody时,您的代码无法工作,因为您的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";
  }
}
 类似资料:
  • 我试图使用Jquery发送一个Ajax POST请求,但我有400个错误请求。 这是我的代码: 它说:无法根据请求构建资源。我错过了什么?

  • 我在Eclipse Photon上用Java8编写了一个简单的webService,使用RestTemplate发布(使用postForObject)一个对象(称为patentListWrapper),该对象包装了一个对象列表(称为PatentDetails)。我从Java客户机(称为MainWsClient)发布,然后在服务器端的patentDetails中设置一个值,并在客户机中读取paten

  • 我在用Retrofit2。发送一个带有主体的POST请求(作为JSON数组),但得到的是“400 Bad request”。同时,从SoapUI和我的http客户机(基于HttpURLConnection)发送请求也很好。 响应的内容类型是text/plain,但应该是application/json 例如,使用SOAPUI的请求/响应:请求: 及其回应: null 这是服务器endpoint:

  • 目标是通过ajax将对象发送到另一台服务器,这是我为CORS设置的,下面是代码。ajax代码片段: 正确发布数据,但使用

  • 我正在使用实现一个联系人应用程序。现在,我正试图通过发送以下格式的put请求来更新联系人 我将XML作为字符串发送,作为请求的主体。这是我的xmlString(请求主体) 我写了下面的代码来发送更新联系人的PUT请求。 当我试图在中发送请求时,联系人更新成功。但是当我试图运行上面的程序时,我得到了 400错误请求错误 我不知道我哪里出错了。任何帮助都将不胜感激!

  • 问题内容: 这是我的代码: 由此可见: 错误的请求(#400):无法验证您的数据提交。 而且我已经有了。我该如何解决这个问题? 问题答案: 您对enableCsrfValidation有问题。要了解更多信息,可以在这里阅读。 要禁用CSRF,请将以下代码添加到您的控制器中: 这将禁用所有操作。您可能应该根据$ action,仅对特定操作禁用它。