Unable to change UAP soap in envelope using java SoapMessage

I am trying to create a simple SOAP message to send from a client, but I (apparently) cannot change the namespace URI of the "soap" in the envelope.

This is what the soap header should look like:

<soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope/" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding/"> ... </soap:Envelope> 

So, I have the following code:

  final SOAPMessage sm = MessageFactory.newInstance().createMessage(); final SOAPPart sp = sm.getSOAPPart(); final SOAPEnvelope se = sp.getEnvelope(); final SOAPHeader sh = se.getHeader(); final SOAPBody sb = se.getBody(); se.removeNamespaceDeclaration(se.getPrefix()); se.addNamespaceDeclaration("soap", "http://www.w3.org/2001/12/soap-envelope"); se.setPrefix("soap"); sb.setPrefix("soap"); sh.setPrefix("soap"); se.setEncodingStyle("http://www.w3.org/2001/12/soap-encoding/"); 

However, when I print a message before sending, the following is my envelope:

 <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding/"> 

Note the differences in the xmlns: soap URI in the "must be" section and the actual one.

If I change the first argument of calling addNamespaceDeclaration to soap instead of soap, this is the next envelope I get:

 <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapy="http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding/"> 

I assume this may be due to the fact that the addNamespaceDeclaration call is not something like changeNamespaceDeclaration , and it is ignored, given that the namespace is already present, but I cannot find something that works (I already tried setAttributeNS ).

EDIT: I just realized that setAttributeNS stupid because it is a namespace change, not a URI. EDIT AGAIN: I'm a little confused as I keep searching, sometimes I see that the naming goes soap:"Namespace" , so in that sense I want to change the namespace ... but I thought the namespace is a "soap", part. Any clarifications?

This is my first post, so I apologize if I ask for something that has already been solved, but I searched for it, and most of what I found is related to changing the namespace (for example, from SOAP-ENV, which is the default namespace for soap), not the URI itself. Thanks in advance.

-M

+6
source share
1 answer

In general, you do not need to manually change the SOAP namespace. You probably want to create a SOAP 1.2 message (which has a different namespace than SOAP 1.1). Try to remove all lines that change the namespace from your code and change the first line to

 final SOAPMessage sm = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL).createMessage(); 

If you really need to specify which prefix should be used, this code works:

 SOAPMessage sm = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL).createMessage(); sm.getSOAPPart().getEnvelope().setPrefix("soap"); sm.getSOAPPart().getEnvelope().removeNamespaceDeclaration("env"); sm.getSOAPHeader().setPrefix("soap"); sm.getSOAPBody().setPrefix("soap"); 
+13
source

Source: https://habr.com/ru/post/913966/


All Articles