4

I'm having an issue with jaxb. One of my elements is requiring me to set a JAXBElement<String>, instead of just a String. I know that a solution to this is to change the xsd, as discussed here JAXB generating JAXBElement<String> instead of String, but the xsd was provided by the vendor, and we can't convince them to change it.

Apparently another solution is to specify a property <jaxb:globalBindings generateElementProperty="false">, which would turn off the JAXBElements globally, which is also not what we want.

According to this page, it should be possible to set generateElementProperty="false" locally in a <jaxb:property> customization. How can I do that in a bindings file? The only examples I can find (like this) seem to be setting it directly in the xsd -- and if I could change that, I'd just remove the minOccurs.

My current bindings file looks like this:

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

The offending portion of the XSD looks like this:

  <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>
Community
  • 1
  • 1
Mike H
  • 63
  • 5

1 Answers1

4

You should be able to set it locally in your external binding like this

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

Make sure the XPath expression in jxb:bindings matches!

schnatterer
  • 6,520
  • 6
  • 53
  • 71