简体   繁体   中英

How to add childElements without prefix to Soap header?

I need to add header elements to a Soap Request, but the child elements inside the header dont have any prefix defined. When I try to add the element without specifing a prefix, this throws a exception.

private SOAPHeader addSecuritySOAPHeader(SOAPMessageContext context) {
SOAPEnvelope envelope = context.getMessage().getSOAPPart().getEnvelope();
envelope.addNamespaceDeclaration("S", "http://schemas.xmlsoap.org/soap/envelope/");
envelope.addNamespaceDeclaration("SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/");

SOAPEnvelope header = envelope.getHeader();
// ACTION NODE
SOAPElement action = header.addChildElement("Action");
return header;
}

Last line produces next exception "com.sun.xml.messaging.saaj.SOAPExceptionImpl: HeaderElements must be namespace qualified"

Heaser i need to create:

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
  <S:Header>
    <Action xmlns="http://schemas.xmlsoap.org/ws/2004/08/addressing">http://cgbridge.rategain.com/2011A/ReservationService/HotelResNotif</Action>
  </S:Header>
  ..............
</S:Envelope>

If I include any prefix, like S, request fail, server response with "Bad request"

How can i add a "clean" Action node?

Is I add a prefix in action: SOAPElement action = header.addChildElement("Action","S"); Service responses with a "Bad request" message.

<S:Action xmlns="http://schemas.xmlsoap.org/ws/2004/08/addressing">http://cgbridge.rategain.com/2011A/ReservationService/HotelResNotif</S:Action>

Any help, please?

This should work:

@Test
public void someTest() throws Exception {
    MessageFactory messageFactory = MessageFactory.newInstance();
    SOAPMessage soapMessage = messageFactory.createMessage();

    SOAPEnvelope soapEnvelope = soapMessage.getSOAPPart().getEnvelope();
    var header = soapEnvelope.getHeader();
    var actionElement = header.addChildElement("Action", "prefix", "http://schemas.xmlsoap.org/ws/2004/08/addressing");
    actionElement.addTextNode("http://cgbridge.rategain.com/2011A/ReservationService/HotelResNotif");

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    soapMessage.writeTo(out);
    System.out.println(new String(out.toByteArray()));
}

Prints:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header><prefix:Action xmlns:prefix="http://schemas.xmlsoap.org/ws/2004/08/addressing">http://cgbridge.rategain.com/2011A/ReservationService/HotelResNotif</prefix:Action></SOAP-ENV:Header><SOAP-ENV:Body/></SOAP-ENV:Envelope>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM