• 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
  • 相关阅读:
    codechef Dynamic GCD [树链剖分 gcd]
    bzoj 4546: codechef XRQRS [可持久化Trie]
    bzoj 4835: 遗忘之树 [树形DP]
    bzoj 4033: [HAOI2015]树上染色 [树形DP]
    bzoj 4591: [Shoi2015]超能粒子炮·改 [lucas定理]
    3167: [Heoi2013]Sao [树形DP]
    bzoj 3812: 主旋律 [容斥原理 状压DP]
    有标号的二分图计数 [生成函数 多项式]
    有标号DAG计数 [容斥原理 子集反演 组合数学 fft]
    BZOJ 3028: 食物 [生成函数 隔板法 | 广义二项式定理]
  • 原文地址:https://www.cnblogs.com/yangzhilong/p/3302693.html
Copyright © 2020-2023  润新知