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; 
    } 
} 

请您参考如下方法:

语法不正确意味着json有问题,请将单引号替换为双引号。

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

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


评论关闭
IT干货网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!