Change the prefix in CXF with interceptor

半世苍凉 提交于 2019-12-12 08:59:32

问题


I am trying to change

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

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

I am using Mule and CXF. We are exposing a SOAP service and the wsdl is from a legacy system (we imported it and generated the classes). It is necessary to change the prefix from 'soap' to just 's'. I see that this is possible with an Interceptor, but for some reason I cant make it work. Here is the code for my interceptor:

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

}

Furthermore, can I change the prefixes for the schemas inside the response?


回答1:


After some testing around, it worked. It might seem very stupid and my jaw dopped a couple of times, I restarted, cleared all cash, re-built and it seems like the interceptor works WHEN I add an extra line, which is unbelievable, I know:

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

I had to drink some coffee, take some time before I got it that it actually is working this way. So, more or less what they suggest here:

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



来源:https://stackoverflow.com/questions/30081435/change-the-prefix-in-cxf-with-interceptor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!