
本文旨在解决使用CXF 3.4.2 Maven插件生成SOAP客户端存根时,遇到的No ObjectFactory with an @XmlElementDecl错误。该错误通常发生在尝试创建服务存根时,提示缺少与特定XML元素关联的ObjectFactory。本文将提供详细的解决方案,通过配置命名空间映射,避免与其他客户端生成的代码冲突,从而解决此问题。
在使用CXF Maven插件生成SOAP客户端代码后,在初始化服务时可能会遇到类似以下的错误信息:
There's no ObjectFactory with an @XmlElementDecl for the element {http://tempuri.org/}travelDocumentNumber.
this problem is related to the following location:
    at protected javax.xml.bind.JAXBElement org.tempuri.GetActiveVisasForArrival.travelDocumentNumber
    at org.tempuri.GetActiveVisasForArrival即使ObjectFactory中已经存在对应的声明,例如:
@XmlElementDecl(namespace = "http://tempuri.org/", name = "travelDocumentNumber", scope = GetActiveVisasForArrival.class)
public JAXBElement<String> createGetActiveVisasForArrivalTravelDocumentNumber(String value) {
    return new JAXBElement<String>(_GetActiveVisasForArrivalTravelDocumentNumber_QNAME, String.class, GetActiveVisasForArrival.class, value);
}该错误仍然会发生。这通常表明命名空间和Java包之间存在冲突。
解决此问题的关键在于确保WSDL中定义的XML命名空间与生成的Java包之间存在明确的映射关系。通过在CXF Maven插件配置中添加命名空间映射,可以避免命名冲突,从而解决No ObjectFactory错误。
在pom.xml文件中,找到CXF Maven插件的配置,并添加<extraarg>元素来指定命名空间与Java包的映射关系。
<plugin>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-codegen-plugin</artifactId>
    <version>3.4.2</version>
    <executions>
        <execution>
            <id>generate-sources</id>
            <phase>generate-sources</phase>
            <configuration>
                <sourceRoot>./generated/cxf</sourceRoot>
                <wsdlOptions>
                    <wsdlOption>
                        <wsdl>./src/main/resources/wsdl/evisa.wsdl</wsdl>
                        <serviceName>BorderManagementSystemService</serviceName>
                        <extraargs>
                            <extraarg>-client</extraarg>
                            <extraarg>-verbose</extraarg>
                            <!-- 添加命名空间映射 -->
                            <extraarg>-p</extraarg>
                            <extraarg>http://tempuri.org/=rw.gov.dgie.gk.integration.evisa.client</extraarg>
                            <extraarg>-p</extraarg>
                            <extraarg>http://schemas.migration.gov.rw/evisa=rw.gov.dgie.gk.integration.evisa</extraarg>
                        </extraargs>
                    </wsdlOption>
                </wsdlOptions>
            </configuration>
            <goals>
                <goal>wsdl2java</goal>
            </goals>
        </execution>
    </executions>
</plugin>解释:
重要提示:
在配置了命名空间映射后,重新生成客户端代码,并按照以下方式初始化服务:
service = (IBorderManagementSystemService) new BorderManagementSystemService(new URL(WSDL_LOCATION)).getBasicHttpBindingIBorderManagementSystemService();
通过配置CXF Maven插件中的命名空间映射,可以有效地解决No ObjectFactory with an @XmlElementDecl错误,确保生成的SOAP客户端代码能够正确地与服务端进行交互。在遇到类似问题时,请仔细检查WSDL文件中的命名空间,并确保它们与生成的Java包之间存在清晰的映射关系。 避免由于命名冲突导致的问题,提高代码的可维护性和可靠性。
以上就是解决CXF生成SOAP客户端时缺失ObjectFactory的问题的详细内容,更多请关注php中文网其它相关文章!
                        
                        每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
                Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号