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.