SOAP getBody vs writeTo method

I'm trying to create soapMessage so I can move on to the following code snippet later:

SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance(); SOAPConnection connection = soapConnectionFactory.createConnection(); SOAPMessage response = connection.call(message, url); 

However, I get an empty (null) response: [SOAP-ENV: Body: null].

When I do the following (before calling the connection):

 System.out.println(message.getSOAPBody()); message.writeTo(System.out); 

I get two different answers when they should be the same, right?

The first system print ln gives me [SOAP-ENV: Body: null], and the other actually gives me a message about the soap that I created (writeTo).

Any ideas why?

Full code:

 MessageFactory factory = MessageFactory.newInstance(); SOAPMessage message = factory.createMessage(); SOAPEnvelope envelope = message.getSOAPPart().getEnvelope(); SOAPElement tvl = envelope.addAttribute(new QName("xmlns:tvl"), "http://some.url.com/"); SOAPBody body = message.getSOAPBody(); QName bodyText = new QName("tvl:searchAirings"); SOAPBodyElement bodyElement = body.addBodyElement(bodyText); QName fromTag = new QName("from"); SOAPElement from = bodyElement.addChildElement(fromTag); from.setValue("2012-11-02T14:00:00-4:00"); QName toTag = new QName("to"); SOAPElement to = bodyElement.addChildElement(toTag); to.setValue("2012-11-02T18:00:00-4:00"); QName networkTag = new QName("network"); SOAPElement network = bodyElement.addChildElement(networkTag); network.setAttribute("id", "n501"); network.setAttribute("language", "es"); System.out.println(message.getSOAPBody()); message.writeTo(System.out); 
+4
source share
2 answers

System.out.println(message.getSOAPBody()); => This should just print the body of the SOAP envelope.

message.writeTo(System.out); => This should print the full SOAP message, i.e. envelope, header and body.

+1
source

You can solve this problem by writing a ByteArrayOutputStream answer

 SOAPMessage soapResponse = soapConnection .call(createSOAPRequest(), url); ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream(); soapResponse.writeTo(byteOutStream); String res = new String(byteOutStream.toByteArray()); System.out.println("Response \n"+res); 
0
source

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


All Articles