mapper.readValue不支持JSON视图

时间:2018-12-03 20:52:57

标签: json serialization mapper

我正在使用JSONView隐藏东西,以免被显示给API。如果我想拥有非人类可读的JSON,一切都会正常。问题是,我也在尝试

 prettify  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应该完全从json中隐藏(并且在调用mapper.readValue之前从json字符串中隐藏),它也将'answer'初始化为null:

[ { "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 } ]

这是我的Question类:

@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时的json) [{"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" ]}]

1 个答案:

答案 0 :(得分:0)

我通过将@JsonInclude(JsonInclude.Include.NON_EMPTY)添加到所说的null或空实体中的字段来“修复”此问题。 NON_EMPTY隐藏所有值,如果为空或为空,则NON_NULL仅隐藏空值。我可能一开始就问错了事。