如何将没有前缀的子元素添加到 Soap header?

How to add childElements without prefix to Soap header?

我需要将 header 元素添加到 Soap 请求中,但是 header 中的 child 元素没有定义任何前缀。当我尝试在不指定前缀的情况下添加元素时,这会引发异常。

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;
}

最后一行产生下一个异常 "com.sun.xml.messaging.saaj.SOAPExceptionImpl: HeaderElements must be namespace qualified"

我需要创建的 Heaser:

<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>

如果我包含任何前缀,如 S,请求失败,服务器响应 "Bad request"

如何添加 "clean" 操作节点?

我是不是在action中添加了一个前缀: SOAPElement 操作 = header.addChildElement("Action","S"); 服务响应 "Bad request" 消息。

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

有什么帮助吗?

这应该有效:

@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()));
}

打印:

<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>