IllegalAnnotationExceptions:两个类具有相同的XML类型名称。 CXF插件生成的代码

时间:2019-03-13 12:33:27

标签: java cxf-codegen-plugin java-ws

我有两个WSDL,可使用maven cxf-codegen-plugin(org.apache.cxf)从中生成Java代码。它们都使用相同的名称空间,并且包含相同名称的复杂类型,这就是为什么我遇到与Java类名称冲突的问题的原因。我决定使用bindings.xml解决它:

<jaxws:bindings
        xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
        xmlns:jaxws="http://java.sun.com/xml/ns/jaxws"
        xmlns="http://www.w3.org/2001/XMLSchema"
        version="2.1">
    <jaxws:bindings wsdlLocation="./TaskService_v1.wsdl" node="xsd:schema">
        <jaxb:schemaBindings>
            <jaxb:nameXmlTransform>
                <jaxb:typeName prefix="TaskService" />
                <jaxb:elementName prefix="TaskService" />
            </jaxb:nameXmlTransform>
        </jaxb:schemaBindings>
    </jaxws:bindings>
</jaxws:bindings>

例如,这将生成的类B2BParameter重命名为TaskServiceB2BParameter,因此我不再遇到名称冲突的问题,但遇到了另一个问题:

Unknown JAXB exception; nested exception is com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 4 counts of IllegalAnnotationExceptions
Two classes have the same XML type name "{http://serviceinterface.b2b.adx.com}B2BParameter". Use @XmlType.name and @XmlType.namespace to assign different names to them.
this problem is related to the following location:
    at com.adx.b2b.serviceinterface.TaskServiceB2BParameter
this problem is related to the following location:
    at com.adx.b2b.serviceinterface.B2BParameter
    at protected java.util.List com.adx.b2b.serviceinterface.B2BRequest.parameter
    at com.adx.b2b.serviceinterface.B2BRequest
...

这是生成的TaskServiceB2BParameter.java:

package com.axd.b2b.serviceinterface_v1;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "B2BParameter", propOrder = {
    "name",
    "value"
})
public class TaskServiceB2BParameter {
    ...
}

如何更新我的bindings.xml以适当地更改@ XmlType.name?还是我真的需要更改目标软件包? 预先谢谢你!

1 个答案:

答案 0 :(得分:0)

问题在于,单个编组人员找到了具有相同元素名称的类。我这样解决了这个问题:

1)将类生成到不同的包中

        <plugin>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-codegen-plugin</artifactId>
            <version>3.3.3</version>
            <executions>
                <execution>
                    <id>generate-sources</id>
                    <phase>generate-sources</phase>
                    <configuration>
                        <sourceRoot>${project.build.directory}/generated-sources/cxf</sourceRoot>
                        <wsdlOptions>
                            <wsdlOption>
                                <wsdl>${basedir}/src/main/resources/META-INF/wsdl/Service1.xml</wsdl>
                                <packagenames>
                                    <packagename>com.xxx.service1</packagename>
                                </packagenames>
                            </wsdlOption>
                            <wsdlOption>
                                <wsdl>${basedir}/src/main/resources/META-INF/wsdl/Service2.xml</wsdl>
                                <packagenames>
                                    <packagename>com.xxx.service2</packagename>
                                </packagenames>
                            </wsdlOption>
                        </wsdlOptions>
                    </configuration>
                    <goals>
                        <goal>wsdl2java</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

2)为每个服务创建特殊的marshaller bean

@Configuration
public class WebServiceConfig {

@Bean
public Jaxb2Marshaller service1Marshaller() {
    Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
    marshaller.setPackagesToScan("com.xxx.service1");
    return marshaller;
}

@Bean
public Jaxb2Marshaller service2Marshaller() {
    Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
    marshaller.setPackagesToScan("com.xxx.service2");
    return marshaller;
}
}