【发布时间】:2021-06-28 16:42:18
【问题描述】:
我正在尝试将 json 发送到我的 api,在多次尝试其他解决方案之后,我真的陷入困境,再也找不到任何解决方案了。我通过以下函数收到了一个电话留言
@PostMapping(path = "ts/sts")
public void saveTestStep(@RequestBody TestStepDTO testStepDTO){
TestStep testStep = new TestStep(testStepDTO.getTestCaseId(),
testStepDTO.getStepNumber(),
testStepDTO.getDescription(),
testStepDTO.getTestdata(),
testStepDTO.getPrerequisite(),
testStepDTO.getResult());
TestStepService testStepService = new TestStepService();
testStepService.save(testStep);
}
我用下面的 javascript 脚本调用它
for(i=0;i<prerquisites.length;i++){
b = i+1;
$.ajax({
url: 'http://localhost:8080/ts/sts',
dataType: 'json',
type: 'post',
contentType: 'application/json',
data: [{ "result": result[i], "stepNumber": b, "description": description[i],"testCaseId": 1 , "testdata": testdata[i], "prerequisite": prerquisites[i]}] ,
processData: false,
success: function( data, textStatus, jQxhr ){
$('#response pre').html( JSON.stringify( data ) );
},
error: function( jqXhr, textStatus, errorThrown ){
console.log( errorThrown );
console.log("testcaseid: " + testcaseid);
}
});
}
我有我的 DTO 课程
public class TestStepDTO {
@NotBlank
private Integer testCaseId;
@NotBlank
private Integer stepNumber;
@NotBlank
private String description;
@NotBlank
private String result;
@NotBlank
private String testdata;
@NotBlank
private String prerequisite;
public Integer getTestCaseId() {
return testCaseId;
}
public void setTestCaseId(Integer testCaseId) {
this.testCaseId = testCaseId;
}
public Integer getStepNumber() {
return stepNumber;
}
public void setStepNumber(Integer stepNumber) {
this.stepNumber = stepNumber;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
public String getTestdata() {
return testdata;
}
public void setTestdata(String testdata) {
this.testdata = testdata;
}
public String getPrerequisite() {
return prerequisite;
}
public void setPrerequisite(String prerequisite) {
this.prerequisite = prerequisite;
}
}
但是当我运行它时,我得到以下错误
2021-04-01 18:49:06.043 WARN 28296 --- [io-8080-exec-10] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of `be.
tquality.TestManager.RestService.POJO.TestStepDTO` out of START_ARRAY token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `be.tquality.TestManager.RestService.POJO.T
estStepDTO` out of START_ARRAY token
有人知道或看到我在这个过程中做错了什么吗?
【问题讨论】:
-
该错误为您提供了一个线索:
Cannot deserialize instance of 'be.tquality.TestManager.RestService.POJO.TestStepDTO' out of START_ARRAY tokenTestStepDTO 不是数组类型,而是您将其作为数组传递给服务器。 -
我不是 UI 人员,但您能以某种方式记录/查看实际的 json。您从 UI 生成,在它到达后端?这很可能表明您生成了一个 array,与错误状态完全相同。
-
当你想用 $.ajax 发布 json 时,你必须自己对对象进行字符串化
标签: javascript java json spring-boot