Note that there are some explanatory texts on larger screens.

plurals
  1. POCustomising JAX-WS prefix of a SOAP response
    text
    copied!<h2>Goal</h2> <p>I'm implementing a web service for quite an old (but sadly unchangeable) interface. I have an issue where the client that is calling my service expects a certain namespace in the SOAP response and I'm having difficulty in changing it to match.</p> <p>Considering a hello world example, I want this:</p> <pre><code>&lt;S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"&gt; &lt;S:Body&gt; &lt;ns2:helloResponse xmlns:ns2="http://test/"&gt; &lt;return&gt;Hello Catchwa!&lt;/return&gt; &lt;/ns2:helloResponse&gt; &lt;/S:Body&gt; &lt;/S:Envelope&gt; </code></pre> <p>to look like this:</p> <pre><code>&lt;S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"&gt; &lt;S:Body&gt; &lt;customns:helloResponse xmlns:customns="http://test/"&gt; &lt;return&gt;Hello Catchwa!&lt;/return&gt; &lt;/customns:helloResponse&gt; &lt;/S:Body&gt; &lt;/S:Envelope&gt; </code></pre> <p>I found something similar to what I'm trying to do <a href="https://stackoverflow.com/a/5719285/79450">here</a> but I'm having trouble getting similar code to execute properly. (I'd like to stick with Metro and not have to change to cxf or axis)</p> <hr> <h2>Execution</h2> <p>My implementation of <a href="http://javasourcecode.org/html/open-source/jdk/jdk-6u23/com/sun/xml/internal/ws/developer/JAXBContextFactory.html" rel="nofollow noreferrer"><code>JAXBContextFactory</code></a> that returns a <a href="http://javasourcecode.org/html/open-source/jdk/jdk-6u23/com/sun/xml/internal/bind/api/JAXBRIContext.html" rel="nofollow noreferrer"><code>JAXBRIContext</code></a> looks like this:</p> <pre><code>import com.sun.xml.bind.api.JAXBRIContext; import com.sun.xml.bind.api.TypeReference; import com.sun.xml.ws.api.model.SEIModel; import com.sun.xml.ws.developer.JAXBContextFactory; import java.util.ArrayList; import java.util.List; import javax.xml.bind.JAXBException; import javax.xml.namespace.QName; public class HelloJaxbContext implements JAXBContextFactory { @Override public JAXBRIContext createJAXBContext(SEIModel seim, List&lt;Class&gt; classesToBind, List&lt;TypeReference&gt; typeReferences) throws JAXBException { List&lt;Class&gt; classList = new ArrayList&lt;Class&gt;(); classList.addAll(classesToBind); List&lt;TypeReference&gt; refList = new ArrayList&lt;TypeReference&gt;(); for (TypeReference tr : typeReferences) { refList.add(new TypeReference(new QName(tr.tagName.getNamespaceURI(), tr.tagName.getLocalPart(), "customns"), tr.type, tr.annotations)); } return JAXBRIContext.newInstance(classList.toArray(new Class[classList.size()]), refList, null, seim.getTargetNamespace(), false, null); } } </code></pre> <p>Some test code for the web service is simply:</p> <pre><code>import com.sun.xml.ws.developer.UsesJAXBContext; import javax.jws.WebService; import javax.jws.WebMethod; import javax.jws.WebParam; @WebService(serviceName = "Hello") @UsesJAXBContext(value = HelloJaxbContext.class) public class Hello { @WebMethod(operationName = "hello") public String hello(@WebParam(name = "name") String txt) { return "Hello " + txt + "!"; } } </code></pre> <hr> <h2>Issues</h2> <p>In Tomcat 7.0.32 and Glassfish 3.1.2 using jaxws-rt 2.2.7 (from Maven), the above code doesn't affect my web service output (the namespace prefix is still "ns2").</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload