将嵌套的json POST到Spring控制器

时间:2017-06-15 06:51:53

标签: java json spring mongodb spring-mvc

我使用Spring,以下是我的控制器代码:

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

但是,POST作为嵌套json时的相同模型会抛出一个说明json不可读异常的HTTP 400异常,并且它试图将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: java.io.PushbackInputStream@6a371c03; 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"
}

感谢任何帮助。我已经尝试了几个小时。先感谢您。

2 个答案:

答案 0 :(得分:0)

  @RequestMapping(value = "/campaigns/addTask", method = RequestMethod.POST)
    public Campaign addTaskToCampaign(@RequestParam Map<String,Object> campaignName,@Valid @RequestBody Task task){

     /* 
      if there is exception then just remove Task put it into json to catch into map.        
      access just as map by campaignName.get("key_name");
     */

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

答案 1 :(得分:0)

我要向我的任何开发者发表的第一条评论是:请您重命名boolean。这是因为getter看起来像:isIsText()

作为一般规则,最好避免使用&#34; get&#34;,&#34; set&#34;或&#34;&#34;来启动字段名称。

这是因为这些是java-beans属性的前缀。