JSON映射问题

时间:2018-06-09 16:27:28

标签: java json

我试图映射json输入,但它没有被识别。该请求仍显示为null。不确定为什么会发生这种情况。我尝试了不同的jsons,但它是一样的。任何人都可以告诉为什么会发生这种情况。其他请求工作正常。只有列表不起作用。

控制器

@RequestMapping(method = RequestMethod.POST, path = "/addQuestionOptions")
    public void addQuestionOptions(@RequestBody QuestionOptionsRequest questionOptionsRequest,
                                   @RequestParam("questionId") int questionId) {
        questionService.addQuestionOptions(questionOptionsRequest, questionId);
    }

请求

@JsonInclude(JsonInclude.Include.NON_NULL)
public class QuestionOptionsRequest {

    private List<QuestionOption> questionOptions;

    public List<QuestionOption> getQuestionOptions() {
        return questionOptions;
    }

    public void setQuestionOptions(List<QuestionOption> questionOptions) {
        this.questionOptions = questionOptions;
    }   
}

QuestionOption

@Entity
@JsonIgnoreProperties({"quesiton"})
public class QuestionOption {

    @Id
    @GeneratedValue
    private String id;

    private String questionOptionId;

    private String questionOption;

    public Question getQuestion() {
        return question;
    }

    public void setQuestion(Question question) {
        this.question = question;
    }

    @ManyToOne(cascade=CascadeType.ALL, fetch = FetchType.LAZY)
    @MapsId
    private Question question;

    public void setQuestionOptionId(String QuestionOptionId)
    {
        this.questionOptionId = QuestionOptionId;
    }

    public void setQuestionOption(String QuestionOption)
    {
        this.questionOption = QuestionOption;
    }

    public String getQuestionOption()
    {
        return this.questionOption;
    }

    public String getQuestionOptionId()
    {
        return this.questionOptionId;
    }

}

JSON

{ 
    "questionOption" : [
    {
        "questionOptionId" : "1",
        "questionOption" : "A"
    },
    {
        "questionOptionId" : "2",
        "questionOption" : "B"
    },
    {
        "questionOptionId" : "3",
        "questionOption" : "C"
    }
]
}

1 个答案:

答案 0 :(得分:0)

我可以通过将密钥更改为“问题选项”来解决问题。这有效。经验教训!!