【问题标题】:Jaxb: Generate constant value for fixed-value attributeJaxb:为固定值属性生成常量值
【发布时间】:2016-09-03 17:56:58
【问题描述】:

我目前正在开发一个使用以下结构的 xsd:

<xs:attribute name="listVersionID" type="xs:normalizedString" use="required" fixed="1.0">

虽然本身没有问题,但使用起来相当烦人,因为这个定义的固定值在 xsd 规范的版本之间增加,我们需要修改单独的常量类中的值以保持它们有效,尽管 xsd 中的任何有趣的东西都没有改变。 xsd 在其他地方维护,因此仅更改它是没有选择的。

因此我问自己是否有一个 jaxb-plugin 或类似的插件来将固定值属性转换为常量 ala

@XmlAttribute(name = "listVersionID")
@XmlJavaTypeAdapter(NormalizedStringAdapter.class)
@XmlSchemaType(name = "normalizedString")
protected final String listVersionID = "1.0";

而不仅仅是

@XmlAttribute(name = "listVersionID")
@XmlJavaTypeAdapter(NormalizedStringAdapter.class)
@XmlSchemaType(name = "normalizedString")
protected String listVersionID;

必须手动填充。

有人知道吗?

【问题讨论】:

    标签: java xsd jaxb wsdl2java


    【解决方案1】:

    如果您不想修改架构,另一种选择是使用外部绑定文件

    <?xml version="1.0" encoding="UTF-8"?>
    <jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
        version="2.0"
        xmlns:xs="http://www.w3.org/2001/XMLSchema">
    
      <jaxb:bindings schemaLocation="yourschema.xsd" node="/xs:schema">
        <jaxb:globalBindings fixedAttributeAsConstantProperty="true" />
      </jaxb:bindings>
    
    </jaxb:bindings>
    

    这相当于@jmattheis 在他的回答中提出的建议。

    【讨论】:

      【解决方案2】:

      是的,可以通过自定义 jaxb 绑定来实现,它可以作为文件添加到 codegen。

      在 jaxb 绑定中,有fixedAttributeAsConstantProperty-属性。将此设置为 true,指示代码生成器生成带有 fixed 属性作为 java-constants 的属性。

      有两种选择:

      1.通过全局绑定: 然后将所有具有固定值的属性变为常量

      <schema targetNamespace="http://stackoverflow.com/example" 
              xmlns="http://www.w3.org/2001/XMLSchema" 
              xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
              xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
              jaxb:version="2.0">
        <annotation>
          <appinfo>
            <jaxb:globalBindings fixedAttributeAsConstantProperty="true" />
          </appinfo>
        </annotation>
        ...
      </schema>
      

      2。通过本地映射: 它只定义了特定属性的fixedAttributeAsConstantProperty 属性。

      <schema targetNamespace="http://stackoverflow.com/example" 
              xmlns="http://www.w3.org/2001/XMLSchema" 
              xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
              xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
              jaxb:version="2.0">
          <complexType name="example">
              <attribute name="someconstant" type="xsd:int" fixed="42">
                  <annotation>
                      <appinfo>
                          <jaxb:property fixedAttributeAsConstantProperty="true" />
                      </appinfo>
                  </annotation>
              </attribute>
          </complexType>
          ...
      </schema>
      

      这两个例子都应该导致:

      @XmlRootElement(name = "example")
      public class Example {
        @XmlAttribute
        public final static int SOMECONSTANT = 42;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-11-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多