RuntimeAnnoatationReader 读取自定义注解 [英] RuntimeAnnoatationReader to read custom annotations

查看:30
本文介绍了RuntimeAnnoatationReader 读取自定义注解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

RuntimeAnnotationReader 如何用于 JAXB 注释以在运行时读取自定义注释和控制类字段.在谷歌搜索时,我找到了这个链接 http://glassfish.10926.n7.nabble.com/injecting-JAXB-annotations-at-runtime-td59852.html但我无法收集到关于 RuntimeAnnotationReader 的太多信息.我有以下要求:假设我有一个类名 Person.java

How RuntimeAnnotationReader can be used for JAXB annotations to read Custom Annotations and control class fields at runtime.While searching on google i found this link http://glassfish.10926.n7.nabble.com/injecting-JAXB-annotations-at-runtime-td59852.html but i couldn't gather much information about RuntimeAnnotationReader. I have the following reqirement: Suppose I have a class name 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;
}
}

和 student.java:

and student.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 输出时将有效负载显示为标记名,但我希望学生应该出现在标记名处.

While initializing person class object, i am setting payload field as student object. Now while marshalling XML output shows payload as tagname, but I want student should be appear at tagname.

编组输出:

 <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>

P.S.:应该是参数化的类名.在上述情况下,T 应该是 Student.

P.S.: should be the parametrized class name. In the above case T should be Student.

推荐答案

要回答您的问题,请参阅 Annox 以获取自定义 Annotation Reader 实现的示例:

To answer your question, see Annox for an example of a custom Annotation Reader implementation:

https://svn.java.net/svn/annox~svn/trunk/core/src/main/java/org/jvnet/annox/xml/bind/AnnoxAnnotationReader.java

免责声明:我是 Annox 和许多其他 JAXB 实用程序的作者.

Disclaimer: I'm the author of Annox and many other JAXB utilities.

但除此之外,我认为您真的不需要这个运行时技巧来实现您的目标.阅读有关 @XmlElementRef 和 JAXBElement 的更多信息.您可以通过以下有效负载获取 XML:

But apart from that I don't think you really need this runtime tricks to achieve your goal. Read more on @XmlElementRef and JAXBElement. You can get your XML by having a payload like:

@XmlElementRef
JAXBElement<T> payload;

这篇关于RuntimeAnnoatationReader 读取自定义注解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆