4

I have a WSDL + XSD that needs to be turned into Java classes. That's pretty simple - wsimport will handle that without issue. However, I also need to be able to add annotations to the generated classes, and those annotations need to contain information that is contained in the XSD (in particular, they need to reference the xsd:maxLength or xsd:length properties).

Why? Because I plan to transform them into a flat file afterwards, using Bindy. For reference, I know that I can use Annox to add custom annotations to the generated classes, but as far as I'm aware, that would require that either all annotations are identical, with all parameters being identical, or specifying annotations for each element individually, with no way to specify the annotation once along with some way (e.g. xpath) of specifying that the value of one of the parameters should be different for each element.

That is, given a schema extract like

<xsd:element name="smapleRequest">
    <xsd:sequence>
         <xsd:element name="ELEMENT_ONE">
             <xsd:simpleType>
                 <xsd:restriction base="xsd:string">
                     <xsd:length value="3" />
                 </xsd:restriction>
             </xsd:simpleType>
         </xsd:element>
         <xsd:element name="ELEMENT_TWO">
             <xsd:simpleType>
                 <xsd:restriction base="xsd:string">
                     <xsd:maxLength value="8" />
                 </xsd:restriction>
             </xsd:simpleType>
         </xsd:element>
    </xsd:sequence>
</xsd:element>

I would like to see classes that look this:

.
.
.
@FixedLengthRecord
public class SampleRequest {

    @XmlElement(name = "ELEMENT_ONE", required = true)
    @DataField(pos = 1, length=3)
    protected String elementOne;


    @XmlElement(name = "ELEMENT_TWO", required = true)
    @DataField(pos = 4, length=8)
    protected String elementTwo;
    .
    .
    .
}

Ideally, I would like to be able to do this without having to duplicate all the information from the XSD into the JAXB Binding File. I mean, I could, but with potentially hundreds of elements per web service method, and dozens of methods, that would get very, very old very, very fast. At that point, I would probably have to use another tool to generate the XSD and JAXB binding file(s) from the COBOL!

So, does anyone know if this is possible? Have I just missed something in Annox? Or am I just asking for too much here?

4

2 回答 2

1

你有几个选择:XJC 插件是一种途径,而 Annox 看起来很有趣。但是我不是专家,所以我会让其他人与您一起探索。

我建议您考虑的另一条路线,如果您遇到第一个问题,是通过注释处理(以前是 apt 工具,现在是 javac 工具的一部分)对生成的 JAXB 源进行后处理,以访问 XSD 并附加您的即时注释。不确定这是否适用于您的所有情况,但在您给出的示例中,JAXB 生成的注释应该足以构造一个 XPath 表达式来读取相应的 XML 元素类型特征。假设您的需求基本上围绕字段长度,那应该是少数用例和 XPath 表达式。

于 2012-05-29T04:37:29.380 回答
0

要自动添加XJsr303Annotations注释,您可以使用xjc插件https://github.com/krasa/krasa-jaxb-tools

有关详细信息,请参阅我的答案在从 Java JAXB 注释类生成的模式中生成 XSD 限制

于 2019-01-26T10:17:21.667 回答