I've generated source codes for an old Web Service using JAX-WS 2.1.7. When I call this service the generated soap message is something like this:
<env:Envelope xmlns:env='http://schemas.xmlsoap.org/soap/envelope/'>
<env:Header>
</env:Header>
<env:Body>
...
</env:Body>
</env:Envelope>
But the old web service only accepts this format:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
...
</soap:Body>
</soap:Envelope>
As you see the prefix is "soap" instead "env" and there is no header so i got an error complaining about "soap:Body" is required. I can't change the old web service and need to send compatible soap messages. how can i change the prefix to "soap" and also remove "Header"?
You need to create a class that implements SOAPHandler<SOAPMessageContext>
and that includes something like this:
public boolean handleMessage(final SOAPMessageContext context)
{
final Boolean isSoapResponse = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
if (!isSoapResponse)
{
try
{
final SOAPMessage soapMsg = context.getMessage();
soapMsg.getSOAPPart().getEnvelope().setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:soap", "http://schemas.xmlsoap.org/soap/envelope/");
soapMsg.getSOAPPart().getEnvelope().removeAttributeNS("http://schemas.xmlsoap.org/soap/envelope/", "env");
soapMsg.getSOAPPart().getEnvelope().removeAttribute("xmlns:env");
soapMsg.getSOAPPart().getEnvelope().setPrefix("soap");
soapMsg.getSOAPBody().setPrefix("soap");
soapMsg.getSOAPPart().getEnvelope().getHeader().detachNode();
}
catch (SOAPException e)
{
e.printStackTrace();
}
}
return true;
}
Then create a handler.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<handler-chains xmlns="http://java.sun.com/xml/ns/javaee">
<handler-chain>
<handler>
<handler-name>test.MySoapHandler</handler-name>
<handler-class>test.MySoapHandler</handler-class>
</handler>
</handler-chain>
</handler-chains>
And add an annotation to your web service:
@HandlerChain(file = "handler.xml")