微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – 如何将generateElementProperty设置为绑定文件中的本地属性

我遇到了jaxb的问题.我的一个元素是要求我设置一个JAXBElement< String>而不仅仅是一个String.我知道解决这个问题的方法是改变xsd,如JAXB generating JAXBElement<String> instead of String所述,但xsd是由供应商提供的,我们无法说服他们改变它.

显然,另一个解决方案是指定一个属性< jaxb:globalBindings generateElementProperty =“false”>,这将关闭全局JAXBElements,这也不是我们想要的.

根据this page,应该可以在< jaxb:property>中本地设置generateElementProperty =“false”.定制.我怎么能在绑定文件中这样做?我能找到的唯一例子(如this)似乎是直接在xsd中设置它 – 如果我可以改变它,我只需删除minOccurs.

我当前的绑定文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
<jxb:bindings version="1.0" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" xmlns:xs="http://www.w3.org/2001/XMLSchema" jxb:extensionBindingPrefixes="xjc" xmlns:jxb="http://java.sun.com/xml/ns/jaxb">
  <jxb:bindings schemaLocation="../XXXPrivateService.xsd" node="/xs:schema">
    <jxb:globalBindings>
      <xjc:novalidator />
      <xjc:serializable />
    </jxb:globalBindings>
    <jxb:schemaBindings>
      <jxb:package name="com.XXX.YYYY.xsd.XXXprivateservice">
        <jxb:javadoc><![CDATA[<body>Todo</body>]]></jxb:javadoc>
      </jxb:package>
    </jxb:schemaBindings>
  </jxb:bindings>

  <jxb:bindings schemaLocation="../XXXPrivateService_1_2.xsd" node="/xs:schema">
    <jxb:schemaBindings>
      <jxb:package name="com.XXX.YYYY.xsd.XXXprivateservice.two">
        <jxb:javadoc><![CDATA[<body>Todo</body>]]></jxb:javadoc>
      </jxb:package>
    </jxb:schemaBindings>
  </jxb:bindings>

  <jxb:bindings schemaLocation="../XXXPrivateService_1_2_3.xsd" node="/xs:schema">
    <jxb:schemaBindings>
      <jxb:package name="com.XXX.YYYY.xsd.XXXprivateservice.three">
        <jxb:javadoc><![CDATA[<body>Todo</body>]]></jxb:javadoc>
      </jxb:package>
    </jxb:schemaBindings>
  </jxb:bindings>

    <jxb:bindings schemaLocation="../XXXPrivateService_1_2_3_4.xsd" node="/xs:schema">
    <jxb:schemaBindings>
      <jxb:package name="com.XXX.YYYY.xsd.XXXprivateservice.four">
        <jxb:javadoc><![CDATA[<body>Todo</body>]]></jxb:javadoc>
      </jxb:package>
    </jxb:schemaBindings>
  </jxb:bindings>


  <jxb:bindings schemaLocation="../XXXPrivateService_1_2_3_4_5.xsd" node="/xs:schema">
    <jxb:schemaBindings>
      <jxb:package name="com.XXX.YYYY.xsd.XXXprivateservice.five">
        <jxb:javadoc><![CDATA[<body>Todo</body>]]></jxb:javadoc>
      </jxb:package>
    </jxb:schemaBindings>
  </jxb:bindings>

  <jxb:bindings schemaLocation="../XXXPrivateService_1_2_3_4_5_6.xsd" node="/xs:schema">
    <jxb:schemaBindings>
      <jxb:package name="com.XXX.YYYY.xsd.XXXprivateservice.six">
        <jxb:javadoc><![CDATA[<body>Todo</body>]]></jxb:javadoc>
      </jxb:package>
    </jxb:schemaBindings>
  </jxb:bindings>

  <jxb:bindings schemaLocation="../XXXPrivateService_1_2_3_4_5_6_7.xsd" node="/xs:schema">
    <jxb:schemaBindings>
      <jxb:package name="com.XXX.YYYY.xsd.XXXprivateservice.seven">
        <jxb:javadoc><![CDATA[<body>Todo</body>]]></jxb:javadoc>
      </jxb:package>
    </jxb:schemaBindings>
  </jxb:bindings>

</jxb:bindings>

XSD的违规部分如下所示:

  <xs:element name="PayWithPointsRedemption">
    <xs:complexType>
      <xs:sequence>
        <xs:element minOccurs="0" name="requestSourceId" type="xs:unsignedInt"/>
        <xs:element minOccurs="0" name="rewardsAccountId" type="xs:unsignedInt"/>
        <xs:element minOccurs="0" name="XXXReferenceId" nillable="true" type="xs:string"/>
        <xs:element minOccurs="0" name="externalChannelCode" nillable="true" type="xs:string"/>
        <xs:element minOccurs="0" name="payWithPointsRedemptionDetails" nillable="true" type="q4:ArrayOfPayWithPointsRedemptionDetail" xmlns:q4="http://ZZZZZ.YYYY.com/XXXRewards"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

解决方法:

您应该可以在外部绑定中本地设置它

<jxb:bindings schemaLocation="xyz.xsd"
    node="/xs:schema">
    <jxb:schemaBindings>
        <!-- ... -->
    </jxb:schemaBindings>
    <jxb:bindings
        node="/xs:schema/xs:element[@name='PayWithPointsRedemption']/xs:complexType/xs:sequence">
        <jxb:property generateElementProperty="false" />
    </jxb:bindings>
</jxb:bindings>

确保jxb:bindings中的XPath表达式匹配!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。