我需要添加自定义 soap header ,例如登录 我是这样做的
class Foo implements SOAPHandler<SOAPMessageContext> {
public boolean handleMessage(SOAPMessageContext context) {
try {
SOAPMessage soapMsg = context.getMessage();
SOAPEnvelope soapEnv = soapMsg.getSOAPPart().getEnvelope();
soapEnv.addHeader().addAttribute(new QName("login"), "bob");
soapMsg.writeTo(System.out);//tracing OUT
return true;
} catch (SOAPException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
@HandlerChain(file="handler-chain.xml")//I describe Foo in this file
public class GreeterService
通过 tracing out
我得到消息
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Header login="bob"/><S:Body><ns2:sayGoodbye xmlns:ns2="http://example.com/"><arg0>SOAP</arg0></ns2:sayGoodbye></S:Body></S:Envelope>
带标题
<S:Header login="bob"/>
但是服务器收到了没有任何头信息
<?xml version="1.0" ?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><ns2:sayGoodbye xmlns:ns2="http://example.com/"><arg0 xmlns="">SOAP</arg0></ns2:sayGoodbye></S:Body></S:Envelope>
我做错了什么?
最佳答案
几天前我遇到了类似的问题,需要通过标题发送用户 ID。
我在生成代码时使用特殊参数 - wsimport -XadditionalHeaders
解决了这个问题。
关于java - 向 SOAP 消息添加 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5668231/