0

I have 2 xsd schemas and generated java classes from them like this:

        <plugin>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-xjc-plugin</artifactId>
            <executions>
                <execution>
                    <id>generate-sources</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>xsdtojava</goal>
                    </goals>
                    <configuration>
                        <xsdOptions>
                            <xsdOption>
                                <xsd>${basedir}/src/main/resources/xsd/schema/1.xsd</xsd>
                                <bindingFile>${basedir}/src/main/resources/xsd/binding/1.xjb</bindingFile>
                                <packagename>foo.bar.1</packagename>
                            </xsdOption>
                            <xsdOption>
                                <xsd>${basedir}/src/main/resources/xsd/schema/2.xsd</xsd>
                                <bindingFile>${basedir}/src/main/resources/xsd/binding/2.xjb</bindingFile>
                                <packagename>foo.bar.2</packagename>
                            </xsdOption>
                        </xsdOptions>
                    </configuration>
                </execution>
            </executions>
        </plugin>

This schemas has same classes, that's why i sorted them to different packages. I try to create Jaxb2Marshaller for this packages like this:

@Bean
public Jaxb2Marshaller jaxb2Marshaller() {
    Jaxb2Marshaller marshaller = new Jaxb2Marshaller();

    marshaller.setPackagesToScan("foo.bar.1","foo.bar.2");

    marshaller.setMarshallerProperties(new HashMap<String, Object>() {{
        put(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, true);
    }});

    return marshaller;
}

but got the following exception

org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'jaxb2Marshaller' defined in class path resource: Invocation of init method failed; nested exception is 
    org.springframework.oxm.UncategorizedMappingException: Unknown JAXB exception; nested exception is 
    javax.xml.bind.JAXBException: Provider com.sun.xml.internal.bind.v2.ContextFactory could not be instantiated: 
    com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 51 counts of IllegalAnnotationExceptions. 
    The element name {}Initial has more than one mapping.

I think that i should create separate marshaller for each package, but may be exists solution how to do it with one marshaller.

dabdullin
  • 46
  • 1
  • 8
  • It seems the problem comes from the schema, please try to generate java file from xsd by xjc command such as: `xjc -d src -p foo.bar.1 1.xsd` – Nguyen Phung Jul 20 '18 at 04:23
  • @NguyenPhung thank you for answer, but schemas are generated correct and I can create marshaller for schema without exceptions.The problem begins when I try to create marshaller for 2 schemas, because they have same named classes in the same namespace – dabdullin Jul 20 '18 at 10:24
  • the problem is: different packages, same class(element) name, same namescpace (`{}` empty ns), solution: map the packages to different "name spaces" : https://stackoverflow.com/a/40060446/592355 – xerx593 Jan 06 '22 at 23:55

0 Answers0