webservice系统学习笔记6-使用soap的header传递消息

1、显示的使用soap的header传递消息(不推荐使用,会破坏正常的代码结构,推荐使用handler处理)

 @WebResult(name="deleteResult")
    public boolean deleteUser(@WebParam(header=true,name="adminName")String adminName,@WebParam(name="deleteName")String delName);
@Override
    public boolean deleteUser(String adminName, String delName) {
        return true;
    }

wsdl:

xsd文件

调用代码:

 

/**
     * 使用了SOAP的hearder来传递消息(缺点:会改变方法的参数定义)
     */
    @Test
    public void test4(){
        try {
            String namespace = "http://ws01.yzl.com/";
            String wsdlUrl = "http://localhost:8888/ws01?wsdl";
        
            //1、创建服务(Service)
            URL url = new URL(wsdlUrl);
            QName qname = new QName(namespace,"MyServiceImplService");
            Service service = Service.create(url, qname);
            
            //2、创建Dispatch
            //public interface Dispatch<T>extends BindingProviderDispatch 接口提供对动态调用服务端点操作的支持。javax.xml.ws.Service 接口作为创建 Dispatch 实例的工厂。 
            Dispatch<SOAPMessage> dispatch = service.createDispatch(new QName(namespace,"MyServiceImplPort"), SOAPMessage.class, Service.Mode.MESSAGE);
            
            //3.1、创建SOAPMessage
            SOAPMessage message = MessageFactory.newInstance().createMessage();
            SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
            SOAPBody body = envelope.getBody();
            
            //3.2、处理header信息
            SOAPHeader header = envelope.getHeader();
            if(header==null) header = envelope.addHeader();
            QName hname = new QName(namespace,"adminName","nn");
            header.addHeaderElement(hname).setValue("administrator");
            
            
            //4、创建QName来指定消息中传递数据
            QName ename = new QName(namespace,"deleteUser","nn");
            body.addBodyElement(ename);
            message.writeTo(System.out);
  System.out.println();
//5、通过Dispatch传递消息,并返回响应消息 SOAPMessage returnMessage = dispatch.invoke(message); returnMessage.writeTo(System.out);//打印返回消息 System.out.println(); //5、解析返回的SOAP消息的XML Document doc = returnMessage.getSOAPBody().extractContentAsDocument(); //Document doc = returnMessage.getSOAPPart().getEnvelope().getBody().extractContentAsDocument(); String result = doc.getElementsByTagName("deleteResult").item(0).getTextContent(); System.out.println("result is :" + result); } catch (Exception e) { e.printStackTrace(); } }

 结果:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
  <
SOAP-ENV:Header><nn:adminName xmlns:nn="http://ws01.yzl.com/">administrator</nn:adminName></SOAP-ENV:Header>
  <
SOAP-ENV:Body><nn:deleteUser xmlns:nn="http://ws01.yzl.com/"/></SOAP-ENV:Body>
</
SOAP-ENV:Envelope> <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
  <
S:Header/>
  <
S:Body>
    <
ns2:deleteUserResponse xmlns:ns2="http://ws01.yzl.com/"><deleteResult>true</deleteResult></ns2:deleteUserResponse>
  </
S:Body>
</
S:Envelope> result is :true
posted @ 2013-09-05 10:18  自行车上的程序员  阅读(9551)  评论(1编辑  收藏  举报