我正在使用Java中的jax-rs创建REST服务。我有以下型号:
@XmlSeeAlso({Student.class, Teacher.class})
public abstract class Person {
@XmlElement
public String name;
}
@XmlRootElement
public class Student extends Person {
@XmlElement
public int classNo;
}
@XmlRootElement
public class Teacher extends Person {
@XmlElement
public String qualification;
}
我创建了一个类来打包响应,如下所示:
@XmlRootElement
public class PersonResponse {
@XmlElement
public Person person;
}
现在,我有以下资源类:
@Path("person")
public class ApiResource {
@GET
@Path("student")
@Produces(MediaType.APPLICATION_JSON)
public PersonResponse student() {
Student person = new Student();
person.name = "Steve";
person.classNo = 5;
PersonResponse response = new PersonResponse();
response.person = person;
return response;
}
@GET
@Path("teacher")
@Produces(MediaType.APPLICATION_JSON)
public PersonResponse teacher() {
Teacher person = new Teacher();
person.name = "John";
person.qualification = "Math";
PersonResponse response = new PersonResponse();
response.person = person;
return response;
}
}
当我访问student
端点时,我得到以下结果:
{"person":{"type":"student","name":"Steve","classNo":5}}
当我访问teacher
端点时,我得到以下结果:
{"person":{"type":"teacher","name":"John","qualification":"Math"}}
如您所见,为继承的类'type'
和Teacher
的名称派生了一个额外的字段Student
。
我想知道如何禁用自动生成此字段?