RuntimeAnnotationReader 如何用于 JAXB 注释以在运行时读取自定义注释和控制类字段。在谷歌上搜索时,我发现了这个链接http://glassfish.10926.n7.nabble.com/injecting-JAXB-annotations-at-runtime -td59852.html但我无法收集有关 RuntimeAnnotationReader 的太多信息。我有以下要求:假设我有一个类名 Person.java
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlSeeAlso;
@XmlRootElement(name="Person")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlSeeAlso({Student.class})
public class Person <T>{
private T payload;
@XmlElement(name = "Person-Name")
private String name;
public void setPayload(T payload){
this.payload = payload;
}
public T getPayload(){
return payload;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
}
和学生.java:
package RuntimeAnnotation;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Student {
@XmlElement
private String name;
@XmlElement
private String id;
public void setName(String name) {
this.name = name;
}
public String getName(){
return name;
}
public void setId(String id){
this.id = id;
}
public String getId(){
return id;
}
}
在初始化人员类对象时,我将有效负载字段设置为学生对象。现在,虽然编组 XML 输出将有效负载显示为标记名,但我希望学生应该出现在标记名中。
元帅输出:
<Person>
<payload xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="student">
<name>ABC</name>
<id>239423</id>
</payload>
<Person-Name>XYZ</Person-Name>
</Person>
预期输出:
<Person>
<T>
<name>ABC</name>
<id>239423</id>
<T>
<Person-Name>XYZ</Person-Name>
</Person>
PS:应该是参数化的类名。在上述情况下,T 应该是学生。