Web services Apache CXF客户端命名空间位于元素中,而不是信封中

Web services Apache CXF客户端命名空间位于元素中,而不是信封中,web-services,soap,wsdl,cxf,cxf-codegen-plugin,Web Services,Soap,Wsdl,Cxf,Cxf Codegen Plugin,我遇到的问题是ApacheCXF将名称空间放在元素中而不是soap信封中 我使用CodeGenMaven插件从WSDL生成客户机代码 以下是WSDL中的名称空间: xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:soap="http://schem

我遇到的问题是ApacheCXF将名称空间放在元素中而不是soap信封中

我使用CodeGenMaven插件从WSDL生成客户机代码

以下是WSDL中的名称空间:

              xmlns:http="http://schemas.xmlsoap.org/wsdl/http/"
              xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/"
              xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
              xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
              xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xs="http://www.w3.org/2001/XMLSchema"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
触发SOAP请求时,我可以在日志中看到已发送以下消息:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
    <ns1:userRegistrationRequest xmlns:ns1="http://some.com/namespace1">
        <InputParameter xmlns:ns2="http://some.com/namespace1">
            <ns2:Message>
                <ns2:Value xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
                <ns2:HashTable>
                    <ns2:Item key="action">
                        <ns2:Value>some_action</ns2:Value>
                    </ns2:Item>
                </ns2:HashTable>
            </ns2:Message>
        </InputParameter>
    </ns1:userRegistrationRequest>
</soap:Body>

一些行动

问题是,当我构造消息时,代码中的Value元素为null,但在这里它与XSI名称空间一起出现

我期望得到的是这样的结果(Value元素不应该存在,XSI应该在信封元素中):


一些行动


有人知道如何防止CXF生成空值元素并将名称空间放在soap信封元素中吗?

好的,来回答我的问题。这个问题有两个方面

首先,向信封元素添加名称空间。可以通过编写自定义拦截器并在早期阶段注册它来完成。大概是这样的:

import java.util.Map;

import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.Phase;

public class MyInterceptor extends AbstractSoapInterceptor {

    private Map<String, String> nsMap;

    public MyInterceptor(Map<String, String> nsMap) {
        super(Phase.USER_LOGICAL);
        this.nsMap = nsMap;
      }

    public void handleMessage(SoapMessage message) throws Fault {
        message.put("soap.env.ns.map", nsMap); 
        message.put("disable.outputstream.optimization", Boolean.TRUE); 
    }

}
import java.util.Map;
导入org.apache.cxf.binding.soap.SoapMessage;
导入org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor;
导入org.apache.cxf.interceptor.Fault;
导入org.apache.cxf.phase.phase;
公共类MyInterceptor扩展了AbstractSoapInterceptor{
私有地图;
公共MyInterceptor(nsMap){
超级(阶段用户逻辑);
this.nsMap=nsMap;
}
public void handleMessage(SoapMessage消息)引发错误{
message.put(“soap.env.ns.map”,nsMap);
message.put(“disable.outputstream.optimization”,Boolean.TRUE);
}
}
我找到了上述解决方案

其次,要删除空值元素,应该将xsd更改为该元素具有minOccurs=“0”和nillable=“false”

import java.util.Map;

import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.Phase;

public class MyInterceptor extends AbstractSoapInterceptor {

    private Map<String, String> nsMap;

    public MyInterceptor(Map<String, String> nsMap) {
        super(Phase.USER_LOGICAL);
        this.nsMap = nsMap;
      }

    public void handleMessage(SoapMessage message) throws Fault {
        message.put("soap.env.ns.map", nsMap); 
        message.put("disable.outputstream.optimization", Boolean.TRUE); 
    }

}