2011-04-14 120 views
1

我需要添加自定义的肥皂头,就像登录 我做的方式是这样添加标题到SOAP消息

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> 

我做错了什么?

+0

它是由wsimport生成代码吗? – StKiller 2011-04-14 19:30:56

+0

@StKiller,正确。我只添加SOAPHandler。 – 2011-04-14 19:35:08

回答

4

我前几天有类似的问题,有需要通过标题发送用户ID。

我使用特殊参数 - wsimport -XadditionalHeaders生成代码时解决了此问题。

+0

不幸的是,这个选项没有任何影响。它仍然会生成相同的类。 – 2011-04-14 19:56:19

+0

好的,但源wsdl包含标题的定义? – StKiller 2011-04-14 19:59:08

+0

我应该说一点。实际上,在wsdl中没有关于标题的任何信息。这是一种可选的标题,我自己处理。 – 2011-04-14 19:59:09