mapper.readValue 不尊重 JSON 观点

mapper.readValue not honoring JSON Views

我正在使用 JSONView 来隐藏某些内容,以免向 API 显示。如果我想要非人类可读的 JSON,一切正常。问题是,我也在尝试 美化 json 以使其看起来更具可读性。以下方法中的倒数第二行执行此操作:

@RequestMapping(path = "/questions")
public @ResponseBody List<Question> questionListRest() throws IOException { 
    ObjectMapper mapper = new ObjectMapper();
    String result = mapper
      .writerWithView(Views.Public.class)
      .writeValueAsString((List<Question>) questionRepository.findAll());
    List<Question> JsonList = mapper.readValue(result, new TypeReference<List<Question>>(){});
    return JsonList;
}    

但是,它将 'answer' 初始化为 null,即使答案应该完全从 json 中隐藏(并且它在 mapper.readValue 之前的 json 字符串中隐藏被称为):

[ { "questionId" : 6, "questionName" : "Which of the following would you most likely eat?", "questionType" : "checkbox", "values" : [ "A chainsaw", "A table", "An Apple" ], "answers" : null }, { "questionId" : 7, "questionName" : "What countries have you visited", "questionType" : "checkbox", "values" : [ "Finland", "Sweden", "Estonia" ], "answers" : null }, { "questionId" : 8, "questionName" : "Where did you last feel unconfortable", "questionType" : "checkbox", "values" : [ "At a bar", "While coding spring", "While eating an unsliced long sub" ], "answers" : null } ]

这是我的问题class:

@Entity
public class Question {

@JsonIgnore
private static AnswerRepository answerRepository; 

@JsonIgnore
private static CategoryRepository categoryRepository;   

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long questionId;
private String questionName;  
private String questionType; //text, radio, checkbox..
private String[] values;


public Question(String questionName, String questionType, Category category, String[] values) {
    super();
    this.questionName = questionName;
    this.questionType = questionType;
    this.category = category;
    this.setValues(values);
}

public Question(String questionName, String questionType, Category category) {
    super();
    this.questionName = questionName;
    this.questionType = questionType;
    this.category = category;
}   

@ManyToOne(cascade = {CascadeType.MERGE})
@JoinColumn(name = "categoryid")
@JsonBackReference
private Category category;  

@JsonView(Views.Internal.class)
public List<Answer> getAnswers() {
    return answers;
}

public void setAnswers(List<Answer> answers) {
    this.answers = answers;
}

@JsonView(Views.Internal.class)
@OneToMany(cascade = CascadeType.ALL, mappedBy = "question")
@JsonManagedReference
private List<Answer> answers;   

public Question() {
    super();
}
... getters and setters ...

这是调用 readValue 之前的 json(与记录 result 时一样) [{"questionId":6,"questionName":"Which of the following would you most likely eat?","questionType":"checkbox","values":[ "A chainsaw", "A table", "An Apple" ]},{"questionId":7,"questionName":"What countries have you visited","questionType":"checkbox","values":[ "Finland", "Sweden", "Estonia" ]},{"questionId":8,"questionName":"Where did you last feel unconfortable","questionType":"checkbox","values":[ "At a bar", "While coding spring", "While eating an unsliced long sub" ]}]

我 "fixed" 通过将 @JsonInclude(JsonInclude.Include.NON_EMPTY) 添加到所述 null 或空实体中的字段。 NON_EMPTY 隐藏所有值,空值或空值,NON_NULL 仅隐藏空值。我可能一开始就问错了。