Rahul Sharma
Rahul Sharma

Reputation: 5834

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.

Upvotes: 1

Views: 2170

Answers (2)

Matthieu Marc
Matthieu Marc

Reputation: 11

To complete the answer, when using recent version of java, jaxb-bindings.xjb file have some change :

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jaxb:bindings
        xmlns:jaxb="https://jakarta.ee/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="https://jakarta.ee/xml/ns/jaxb https://jakarta.ee/xml/ns/jaxb/bindingschema_3_0.xsd"
        version="3.0">

    <jaxb:globalBindings>
        <xjc:serializable uid="1" />
    </jaxb:globalBindings>
</jaxb:bindings>

I am using spring-boot 3.3.1 with java 21.

And in case you got error :

XJC generation error : can't parse argument number

just add locale en in your pom.xml :

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>jaxb2-maven-plugin</artifactId>
  ...
  <configuration>
    <locale>en</locale>

ref: XJC generation error : can't parse argument number

Upvotes: 1

Fran&#231;ois Vanhille
Fran&#231;ois Vanhille

Reputation: 114

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>

Upvotes: 4

Related Questions