用拦截器更改 CXF 中的前缀

Change the prefix in CXF with interceptor

我正在努力改变

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body>

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:Body>

我正在使用 Mule 和 CXF。我们公开了一个 SOAP 服务,wsdl 来自遗留系统(我们导入它并生成了 类)。有必要将前缀从 'soap' 更改为 's'。我看到拦截器可以做到这一点,但由于某种原因我无法让它工作。这是我的拦截器的代码:

package se.comaround.interceptors;

import java.util.HashMap;
import java.util.Map;

import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;

public class ComAroundSoapResponceInterceptor extends
  AbstractPhaseInterceptor<SoapMessage> {

 public ComAroundSoapResponceInterceptor() {
  super(Phase.PREPARE_SEND);
 }

 public void handleMessage(SoapMessage message) {
  Map<String, String> hmap = new HashMap<String, String>();
  hmap.put("s", "http://schemas.xmlsoap.org/soap/envelope/");
  message.setContextualProperty("soap.env.ns.map", hmap);
  message.setContextualProperty("disable.outputstream.optimization", true);

  System.out.println("Set up");
 }

}

此外,我可以更改响应中模式的前缀吗?

经过一番测试,它成功了。这可能看起来很愚蠢,我的下巴被吸了几次,我重新启动,清除所有现金,重新构建,当我添加额外的行时,拦截器 似乎工作,这令人难以置信,我知道 :

package se.comaround.interceptors;

import java.util.HashMap;
import java.util.Map;

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

public class ComAroundSoapResponceInterceptor  
        extends  AbstractSoapInterceptor {

    public ComAroundSoapResponceInterceptor() {
        super(Phase.PREPARE_SEND);
    }

    public void handleMessage(SoapMessage message) {
        Map<String, String> hmap = new HashMap<String, String>();
        hmap.put("s", "http://schemas.xmlsoap.org/soap/envelope/");
                message.put("soap.env.ns.map", hmap);
                message.put("disable.outputstream.optimization", true);
    }
}

我不得不喝了些咖啡,花了一些时间才明白它实际上是这样工作的。所以,或多或少他们在这里建议:

http://cxf.547215.n5.nabble.com/How-to-customize-namespaces-position-and-prefix-in-CXF-response-td3423069.html