我使用Spring,以下是我的控制器代碼:反序列化的異常而POST操作嵌套JSON春控制器
@RequestMapping(value = "/campaigns/addTask", method = RequestMethod.POST)
public Campaign addTaskToCampaign(@RequestParam(value = "campaignName")String campaignName,@Valid @RequestBody Task task){
Campaign campaign = campaignInterface.findByName(campaignName);
if (campaign!=null){
List<String> task_ids;
if (campaign.getTask_ids()==null){
task_ids = new ArrayList<>();
}else{
task_ids= campaign.getTask_ids();
}
Task newTask = taskInterface.save(task);
task_ids.add(newTask.getId());
campaign.setTask_ids(task_ids);
return campaignInterface.save(campaign);
}
return null;
}
在哪裏我的任務模式是:
@Document(collection = "tasks")
public class Task {
@Id
private String id;
private String name;
private int points;
private List<Question>questions;
private List<String>answers;
.....
}
和嵌套問題模型:
public class Question {
private Boolean isText= false;
private String questionText;
}
但是,相同的模型時,作爲嵌套json發佈引發HTTP 400異常說json不可讀異常,並且它試圖將String questionText字段解析爲一個布爾值。
這裏是什麼即時通訊發佈:
{
"name" : "Test Task 3",
"questions": [{ "questionText":"What is the name you college festival?","isText":true}]
}
和隨之而來的例外情況是這樣的:
{
"timestamp": 1497508476467,
"status": 400,
"error": "Bad Request",
"exception": "org.springframework.http.converter.HttpMessageNotReadableException",
"message": "JSON parse error: Can not deserialize value of type boolean from String \"What is the name you college festival?\": only \"true\" or \"false\" recognized; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not deserialize value of type boolean from String \"What is the name you college festival?\": only \"true\" or \"false\" recognized\n at [Source: [email protected]; line: 3, column: 32] (through reference chain: com.frapp.CBM.prod.model.Task[\"questions\"]->java.util.ArrayList[0]->com.frapp.CBM.prod.model.Question[\"questionText\"])",
"path": "/campaigns/addTask"
}
任何幫助表示讚賞。我一直在嘗試幾個小時。先謝謝你。
你試試重建嗎? – Jerry06
是的。我從生成菜單重建。我也做了兩次運行。 – MadScientist
從解析錯誤,它似乎期待questionText的布爾值。請在服務器代碼中檢查questionText的數據類型。 – Sumit