Jaxb2Marshaller用于具有相同类的多个软件包

时间:2018-07-19 12:47:24

标签: java spring jaxb spring-oxm

我有2个xsd模式,并从中生成了Java类,如下所示:

        <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>

此架构具有相同的类,这就是为什么我将它们排序到不同的包。 我尝试为该程序包创建Jaxb2Marshaller,如下所示:

@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;
}

但有以下例外情况

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.

我认为我应该为每个程序包创建一个单独的封送处理程序,但是可能存在一个如何使用一个封送处理程序的解决方案。

0 个答案:

没有答案