How to generate plural names with jaxb2

1.5k Views Asked by At

I did find How do you customize how JAXB generates plural method names? , but the question is over 10 years old now and since I had to change my binding to Jakarta the accepted answer doesn't work for me anymore. (Altho I'm not really familiar with jaxb so perhaps I'm wrong)

So after upgrading my jaxb2-maven-plugin version I got an error on the binding file:

not an external binding file. The root element must be {https://jakarta.ee/xml/ns/jaxb}bindings but it is {http://java.sun.com/xml/ns/jaxb}bindings

After googling and some trial and error I managed to get the generation working again with the following binding file

<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings xmlns:jaxb="https://jakarta.ee/xml/ns/jaxb"
               xmlns:xjc="https://jakarta.ee/xml/ns/jaxb"
               xmlns:xs="http://www.w3.org/2001/XMLSchema"
               version="3.0">

  <jaxb:globalBindings>
      <jaxb:javaType name="java.util.Calendar"
          parseMethod="jakarta.xml.bind.DatatypeConverter.parseDateTime"
          printMethod="jakarta.xml.bind.DatatypeConverter.printDateTime"
          xmlType="xs:dateTime"/>
      <xjc:serializable uid="-1"/>
  </jaxb:globalBindings>
</jaxb:bindings>

However, with the java.sun.com bindings collections used to be generated in plural form. With jakarta it seems to be in the singular form. For Example: With sun.com binding collections used to be generated as:

@XmlElement(name = "Strd")
protected List<GeneratedObject> strds;

And with jakarta:

@XmlElement(name = "Strd")
protected List<StructuredRemittanceInformation7> strd;

While changing the references to these fields won't take too much time (I only use the generated object once to map to a dto), I'm still quite curious how I could achieve generating collections with plural forms.

1

There are 1 best solutions below

0
On

Did you change your generating tool by moving to Jakarta ?

You should try the following method for pluralization issues : Eclipse XJC documentation - Experimental simpler & better binding mode

You'll then have a binding file with the following content (magic is done with the <xjc:simple/> tag and jaxb:extensionBindingPrefixes="xjc" declaration)

<jaxb:bindings version="3.0"
 xmlns:jaxb="https://jakarta.ee/xml/ns/jaxb"
 xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
 jaxb:extensionBindingPrefixes="xjc">
  <jaxb:globalBindings>
    <jaxb:javaType name="java.util.Calendar"
          parseMethod="jakarta.xml.bind.DatatypeConverter.parseDateTime"
          printMethod="jakarta.xml.bind.DatatypeConverter.printDateTime"
          xmlType="xs:dateTime"/>
    <xjc:serializable uid="-1"/>
    <xjc:simple />
  </jaxb:globalBindings>
</jaxb:bindings>