【问题标题】:Change the prefix in CXF with interceptor使用拦截器更改 CXF 中的前缀
【发布时间】:2015-07-16 20:48:46
【问题描述】:

我正在尝试改变

<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");
	}

}

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

【问题讨论】:

    标签: soap cxf mule mule-studio


    【解决方案1】:

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

    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

    【讨论】:

    • 您好,实际上,如果您将 message.setContextualProperty 替换为 message.put,则无需调用 message.getEnvelopeNs()。事实上,SoapMessage 扩展了 java Map,如果你直接调用 put,就不会检查现有的键。
    猜你喜欢
    • 2017-08-02
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 2012-04-21
    • 1970-01-01
    • 1970-01-01
    • 2012-10-03
    • 1970-01-01
    相关资源
    最近更新 更多