java - 如何将不带前缀的 childElements 添加到 Soap 标头?
问题描述
我需要将标题元素添加到 Soap 请求,但标题中的子元素没有定义任何前缀。当我尝试在不指定前缀的情况下添加元素时,这会引发异常。
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,请求失败,服务器响应“错误请求”
如何添加“干净”的操作节点?
我是否在操作中添加前缀: SOAPElement action = header.addChildElement("Action","S"); 带有“错误请求”消息的服务响应。
<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>
推荐阅读
- angular - Angular Firebase:页面刷新后无法获取当前用户
- ios - How to read a text file while developing an ios app using xcode
- scala - 使用 Java api 使用 Spark 从 parquet 读取/访问原始双数组
- angular - 模板驱动形式的 Angular Material Mat-Chip 验证
- python - 如何在tensorflow中获取矩阵中每一行的最大值和次最大值?
- python - 如何仅绑定按键而不是在 Tkinter 中按住键时
- python - Python - 在不停止运行 tkinter mainloop 的情况下中止当前函数
- python - 在原子中运行python
- android - android.provider.Telephony.SMS_RECEIVED 无法在 OREO 中接收。我该如何解决这个问题?
- python - TypeError: __init__() 得到了一个意外的关键字参数“missing_vaules”