cxf 解组错误:意外元素

Luc*_*ile 3 soap cxf unmarshalling

我正在尝试使用 SOAP 服务,使用 maven cxf-codegen-plugin 生成存根。除了丑陋的服务之外,大多数服务一切都很好。在这种情况下,当调用时,服务会发送正确的响应,但我生成的存根无法解组它,生成一个异常,例如(为了简短起见,我将 url 替换为 urlx 之类的名称):

javax.xml.ws.soap.SOAPFaultException:解组错误:意外元素(uri:“url3”,本地:“cap”)。预期元素为 <{url1}descComune>....<{url1}cap>...

实际上,意外字段是扩展的一部分,该扩展具有与扩展元素不同的指定命名空间。这里是命名空间定义:

    <?xml version="1.0" encoding="UTF-8"?>
    <wsdl:definitions ...xmlns:ns1="url1" ... xmlns:ns3="url3" ... targetNamespace="someUrl">
     <wsdl:documentation>WSDatiPersonali</wsdl:documentation>
    <wsdl:types>
        <xs:schema xmlns:ax226="url0" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="url0">
Run Code Online (Sandbox Code Playgroud)

扩展的 xs 就像:

<xs:schema xmlns:ax225="url1" xmlns:ax227="url0" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="url1">
            <xs:import namespace="url0"/>
            <xs:complexType name="Indirizzo">
                <xs:sequence>
                    <xs:element minOccurs="0" name="cap" nillable="true" type="xs:string"/>

            </xs:sequence>
        </xs:complexType>
Run Code Online (Sandbox Code Playgroud)

扩展的是:

<xs:schema xmlns:ax224="url3" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="url3">
    <xs:complexType name="Indirizzo">
                <xs:complexContent>
                    <xs:extension base="ns1:Indirizzo">
                        <xs:sequence>
                         ............

                        </xs:sequence>
                    </xs:extension>
                </xs:complexContent>
     </xs:complexType>
</xs:schema>
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,“cap”字段位于父亲内部,当儿子的字段填充到服务响应中时,cxf 无法在儿子的命名空间下找到它。

有修复它的想法吗?

Luc*_*ile 8

最后我找到了某种禁用肥皂验证的解决方法。实际上可以通过多种方式做到这一点:

  • 在响应类上使用注释,例如:

     @org.apache.cxf.annotations.EndpointProperty(key = "soap.no.validate.parts", value = "true") 
    
    Run Code Online (Sandbox Code Playgroud)
  • 设置服务存根的属性:

     JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
            factory.setServiceClass(YourServiceClass.class);            
            factory.setAddress(this.constants.SOME_URL);
            Map<String, Object> properties = factory.getProperties();
            if(properties==null){
                properties= new HashMap<String, Object>();
            }
    
            properties.put("soap.no.validate.parts", "true");
            /**
            * an other option is : 
            * properties.put("set-jaxb-validation-event-handler", "false");
            */
            factory.setProperties(properties);
            WSDatiPersonaliPortType port = (WSDatiPersonaliPortType) factory.create();
    
    Run Code Online (Sandbox Code Playgroud)

我希望这对其他人有用。