发送帖子请求时,客户端发送的请求在语法上是不正确的

时间:2014-02-26 18:28:34

标签: json rest spring-mvc restful-url

myController中的方法如下所示

@RequestMapping(value="/{processId}/dependents", method=RequestMethod.POST,consumes="application/json")
@ResponseBody
public Dependents postdependent(@ModelAttribute ProcessContext process,@RequestBody Dependent dependent) {
    return process.getDependents().addDependent(dependent);
}

我完全得到并删除了工作。 但每当我发帖子时,我收到客户发送的请求在语法上是不正确的。 邮件请求的JSON:

"{
   'dependentId' : '1003',
   'firstName'   : 'Vishu',
   'lastName'    : 'poodari',
   'birthDate'   : '1970/04/15'
}"

请用单引号尝试所有组合,双打引用一切。

我正在使用rest-shell进行操作。

请找到我的依赖类

public class Dependent {
    private String dependentId;
    private String firstName;
    private String lastName;
    private String birthDate;
    @JsonCreator
    public Dependent(@JsonProperty("dependentId") String dependentId, @JsonProperty("firstName") String firstName, @JsonProperty("lastName")String lastName,
            @JsonProperty("birthDate") String birthDate) {
        this.dependentId = dependentId;
        this.firstName = firstName;
        this.lastName = lastName;
        this.birthDate = birthDate;
    }
    public String getDependentId() {
        return dependentId;
    }
    public void setDependentId(String dependentId) {
        this.dependentId = dependentId;
    }
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public String getBirthDate() {
        return birthDate;
    }
    public void setBirthDate(String birthDate) {
        this.birthDate = birthDate;
    }
}

3 个答案:

答案 0 :(得分:12)

语法错误意味着json有问题,请用double替换单引号。

{"dependentId" : "1003",
   "firstName"   : "Vishu",
   "lastName"    : "poodari",
   "birthDate"   : "1970/04/15"
}

还要检查json键是否应与Dependent类属性名称匹配,并且数据应该可由解析器转换。

答案 1 :(得分:5)

错误*客户端发送的请求在语法上是不正确的“**在大多数情况下意味着jackson无法脱盐(将json字符串转换为对象),因为缺少默认构造函数。

在你的情况下,缺少默认构造函数,你有参数化构造函数,它覆盖默认值,而jackson不能创建对象

public Dependent(@JsonProperty("dependentId") String dependentId, @JsonProperty("firstName") String firstName, @JsonProperty("lastName")String lastName,
        @JsonProperty("birthDate") String birthDate) {      this.dependentId = dependentId;
    this.firstName = firstName;
    this.lastName = lastName;
    this.birthDate = birthDate;
}

将默认构造函数添加到您的类中,一切都将正常工作

public Dependent() {
}

答案 2 :(得分:0)

当使用curl(在dos上)时,我遇到了同样的问题。我需要使用所有双引号,因此屏蔽身体部分内的引号: C:> curl -H“Content-Type:application / json”-X POST -d“{\”id \“:1,\”firstName \“:\”Hans \“,\”lastName \“:\ “Muster \”}“http://localhost:8081/persons

相关问题