给定 Person.java:
public class Person implements Serializable {
private static final long serialVersionUID = 1L;
private String lastName;
private String firstName;
private String middleName;
// ...
public String getLastName() {
return lastName;
}
public String getFirstName() {
return firstName;
}
public String getMiddleName() {
return middleName;
}
}
和 Persons.java:
public class Persons implements Serializable {
private static final long serialVersionUID = 1L;
private final List<Person> persons;
public Persons(List<Person> persons) {
this.persons = persons;
}
public List<Person> getPersons() {
return persons;
}
}
当返回 Persons 的 JSON 响应时,会重复“persons”元素:
{
"persons" : {
"persons" : [ {
"lastName" : "McCartney",
"firstName" : "James",
"middleName" : "Paul"
}, {
"lastName" : "Lennon",
"firstName" : "John",
"middleName" : "Winston"
}, {
"lastName" : "Starkey",
"firstName" : "Richard",
"middleName" : null
}, {
"lastName" : "Harrison",
"firstName" : "George",
"middleName" : null
} ]
}
}
如何删除多余的元素?