json 客户端发送的请求在发送 post 请求时语法错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22050042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
the request sent by the client was syntactically incorrect when sending post requests
提问by Vishwa
The method in myController looks like this
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);
}
My gets and delete work perfectly. But whenever I do a post I am getting the request sent by the client was syntactically incorrect. JSON for post request:
我的获取和删除工作完美。但是每当我发帖时,我都会收到客户端发送的请求在语法上不正确。发布请求的 JSON:
"{
'dependentId' : '1003',
'firstName' : 'Vishu',
'lastName' : 'poodari',
'birthDate' : '1970/04/15'
}"
Please I tried all combinations by using single quotes, doubles quotes everything.
请我使用单引号尝试所有组合,双引号所有内容。
I am using rest-shell for doing the operations.
我正在使用 rest-shell 进行操作。
Please find my Dependent Class
请找到我的从属类
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;
}
}
回答by RPaul
syntactically incorrect means problem with the json,please replace the single quote with double.
语法不正确意味着json有问题,请将单引号替换为双引号。
{"dependentId" : "1003",
"firstName" : "Vishu",
"lastName" : "poodari",
"birthDate" : "1970/04/15"
}
also check the json keys should match with your Dependent class attribute names, and the data should be convertible by the parser.
还要检查 json 键是否应与您的从属类属性名称匹配,并且解析器应可转换数据。
回答by abosancic
Error *The request sent by the client was syntactically incorrect"** in most of the case means that Hymanson is not able to desalinize(convert json string to object) because default constructor is missing.
错误 * 客户端发送的请求在语法上不正确“** 在大多数情况下意味着 Hymanson 无法脱盐(将 json 字符串转换为对象),因为缺少默认构造函数。
In your case there is missing default constructor, you have parameterized constructor which override default and Hymanson is not able create object
在您的情况下,缺少默认构造函数,您具有覆盖默认值的参数化构造函数,而 Hymanson 无法创建对象
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;
}
Add also default constructor into your class and everything will be working
还将默认构造函数添加到您的类中,一切都会正常进行
public Dependent() {
}
回答by Hapes
When using curl (on dos) i had the same problem. I needed to use all double quotes and therefore mask the ones within the body part: C:>curl -H "Content-Type: application/json" -X POST -d "{\"id\":1,\"firstName\":\"Hans\",\"lastName\":\"Muster\"}" http://localhost:8081/persons
使用 curl (在 dos 上)时,我遇到了同样的问题。我需要使用所有双引号,因此屏蔽正文部分中的双引号: C:>curl -H "Content-Type: application/json" -X POST -d "{\"id\":1,\"firstName \":\"Hans\",\"lastName\":\"Muster\"}" http://localhost:8081/persons

