The essence of the problem being solved is not clear. On the one hand, you are saying that “ Everything is working fine ”, but at the same time the “ custom SOAP message ” is for some reason not a valid SOAP .
Regarding the issue. You can take control while sending / receiving SOAP messages using handlers, and access the message presented as a DOM structure. For a web service client, this is done like this. We declare a handler:
public class TestClientHandler implements SOAPHandler<SOAPMessageContext> { public Set<QName> getHeaders() { return null; } public boolean handleMessage(SOAPMessageContext smc) { SOAPMessage message = smc.getMessage(); Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY); try { if (outboundProperty) {//если исходящее } else {//если входящее } } catch (Exception e) { e.printStackTrace(); return false; } return true; } public boolean handleFault(SOAPMessageContext context) { return true; } public void close(MessageContext context) { } }
When creating an instance of a web service client, we add our handler:
List<Handler> list = new ArrayList<Handler>(); list.add(new TestClientHandler()); BindingProvider bp = (BindingProvider) port; bp.getBinding().setHandlerChain(list);
Here port is your web service client.
On the server side, the principle of operation is similar, but the handler is added differently. In the Java class that implements the web service, you should add the @HandlerChain annotation:
@WebService(name = "MyService") @HandlerChain(file = "ws-handler.xml") public class MyService {...
You should create a ws-handler.xml with the following contents:
<handler-chain> <handler> <handler-name>WS Handler</handler-name> <handler-class>my.package.TestClientHandler</handler-class> </handler> </handler-chain>
The ws-handler.xml can be, for example, in the same package as the compiled web service class, i.e. - "next to" him.
By accessing the DOM message structure, for example, by calling the message.getSOAPBody() and message.getSOAPHeader() methods, you can make any modifications to the message. Working with DOM not always simple and clear, you have to deal with this separately, but this is beyond the scope of the question asked.