javaxsdjaxb

How to generate classes using maven jaxb implements serializable


I want my all xjc generated classes implementing serializable interface. After reading solution at post I implemented it but jaxb2-maven-plugin throws below error:

[ERROR] file: mapping.xsd [17,34] org.xml.sax.SAXParseException; systemId: file:mapping.xsd; lineNumber: 17; columnNumber: 34; src-annotation: elements can only contain and elements, but 'globalBindings' was found. at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203) at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:134) at

My xsd sample:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
           xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
           jaxb:extensionBindingPrefixes="xjc"
           attributeFormDefault="unqualified"
           elementFormDefault="qualified">
    <xs:element name="MappingFile" type="MappingFileType">
        <xs:annotation>
            <jaxb:globalBindings>
                <xjc:serializable uid="43538530765l"/>
            </jaxb:globalBindings>
        </xs:annotation>

Maven plugin:

<plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>jaxb2-maven-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <sources>
                        <source>xxxx/yyy/mapping.xsd</source>
                    </sources>
                    <packageName>xx.yy.zz.jaxp</packageName>

                </configuration>
            </plugin>

Is there any dependency that i need to use to avoid this exception? Please sugest.


Solution

  • Your binding file should look like:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <jaxb:bindings
            xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
            xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
            xmlns:xs="http://www.w3.org/2001/XMLSchema"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
            version="2.1">
    
        <jaxb:globalBindings>
            <xjc:serializable uid="1" />
        </jaxb:globalBindings>
    </jaxb:bindings>
    

    Moreover, touch your binding file in a specific directory and reference it in the maven plugin specific configuration. Example:

    <configuration>
        <sources>
          <source>src/main/xjb/xsd</source>
        </sources>
      <packageName>xx.yy.zz.jaxp</packageName>
        <xjbSources>
            <xjbSource>src/main/xjb/jaxb-bindings.xjb</xjbSource>
        </xjbSources>
    </configuration>