我使用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,即使answer应该完全隐藏在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
} ]
这是我的问题课:
@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" ]}]
I通过将@JsonInclude(JsonInclude.Include.NON_EMPTY)
添加到所述null或空实体中的字段来"修复"这一问题。NON_EMPTY隐藏所有值,为空或null,NON_null仅为null值。我可能一开始就问错了事情。