问题描述
用例:
想要在 JAXB 生成的 java 类中的字段中插入自定义注解
Wanna insert custom annotation to fields in java class generated by JAXB
问题:
使用 Annotate 插件 + JAXB [1],能够成功插入自定义注释,但它们是在 getter 方法而不是字段中插入的.Morphia (mongo DB) 注释(我实际上想要插入)但是只能注释 java 字段 [2].
Using Annotate plugin + JAXB [1], am able to successfully insert custom annotations but they are getting inserted at getter method rather than field. Morphia (mongo DB) annotations (that i actually want to insert) however can annotate only java fields [2].
我的测试 xsd:
<xsd:schema xmlns:xsd="http://www.51sjk.com/Upload/Articles/1/0/337/337481_20221117113115594.jpg" xmlns:jaxb="http://www.51sjk.com/Upload/Articles/1/0/337/337481_20221117113116049.jpg" jaxb:version="2.1" xmlns:annox="http://annox.dev.java.net" jaxb:extensionBindingPrefixes="annox"> <xsd:element name="hoo" type="External" /> <xsd:complexType name="External"> <xsd:sequence> <xsd:element name="bar" type="xsd:string" /> <xsd:element name="hoobar" type="xsd:string" /> </xsd:sequence> </xsd:complexType> </xsd:schema>
我的测试绑定x**:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <jaxb:bindings version="2.1" xmlns:jaxb="http://www.51sjk.com/Upload/Articles/1/0/337/337481_20221117113116049.jpg" xmlns:xs="http://www.51sjk.com/Upload/Articles/1/0/337/337481_20221117113115594.jpg" xmlns:annox="http://annox.dev.java.net" jaxb:extensionBindingPrefixes="annox"> <jaxb:bindings schemaLocation="external.xsd" node="/xs:schema"> <jaxb:bindings node="xs:complexType[@name='External']/xs:sequence/xs:element[@name='bar']"> <annox:annotate> <annox:annotate annox:class="java.lang.SuppressWarnings" impl="com.acme.foo.MyFieldBridge"> </annox:annotate> </annox:annotate> </jaxb:bindings>
我生成的 java 片段:
My generated java snippet:
@XmlElement(required = true) protected String bar; @XmlElement(required = true) protected String hoobar; /** * Gets the value of the bar property. * * @return * possible object is * {@link String } * */ @SuppressWarnings({ }) public String getBar() { return bar; }
如您所见,我想注释栏"字段.请指教.如果需要,请询问更多.
As you can see, i want to annotate "bar" field. Please advise. Ask for more if needed.
[1] 使用 Jaxb 或 HyperJaxb 生成 @Indexed 注释
[2] 示例见Morphia的@Id注解
[1] Generate @Indexed annotation using Jaxb or HyperJaxb
[2] For sample see @Id annotation of Morphia
推荐答案
好吧,你自己想通了.使用 <annox:annotate target="field"> 注释字段.其他选项是:
Ok, you figured it out yourself. Use <annox:annotate target="field"> to annotate a field. Other options are:
- 二传手
- 设置器参数
- 吸气剂
- 字段
- 类
请参阅文档.