webservice 入门笔记四发送header信息

有时候,webservice方法传递的信息是放在header中的。比如权限认证信息等。

 

在接口中增加一个方法:

@WebResult(name="user")
   public List<User> list(@WebParam(header=true, name="anthInfo")String authInfo);
 

实现如下:

@Override
   public List<User>list(String authInfo) {
      System.out.println(authInfo);
      if("123456".equals(authInfo)){
         returnusers;
      }else {
         System.out.println("auth error!");
         returnnull;
      }
   }


Test方法如下:

 

 /**
          * <p>Title: 带header信息</p>
          * <p>Description: </p>
          * @author zhutulang
          * @version 1.0
          * @throws SOAPException
          * @throws IOException
          * @throws JAXBException
          */
         @Test
         public void test4() throws SOAPException, IOException,JAXBException {
      //1.创建服务
      URL url = new URL(wsdlUrl);
      QName qName = new QName(namespaceUrl, "MyServiceInterImplService");
      Service service = Service.create(url, qName);
     
      //2.创建Dispatch
      Dispatch<SOAPMessage> dispatch =
            service.createDispatch(new QName(namespaceUrl, "MyServiceInterImplPort"), SOAPMessage.class, Service.Mode.MESSAGE);
     
      //3.创建SOAPMessage
      SOAPMessage msg = MessageFactory.newInstance().createMessage();
      SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope();
      SOAPBody body = envelope.getBody();
     
      //header信息
      SOAPHeader header = envelope.getHeader();
      if(header == null){
         header = envelope.addHeader();
      }
      QName hname = new QName(namespaceUrl, "authInfo", "ns");
      header.addHeaderElement(hname).setValue("123456");
     
      //4.创建QName来指定消息中传递的数据
      QName ename = new QName(namespaceUrl, "list", "ns");
      body.addBodyElement(ename);
      msg.writeTo(System.out);
      System.out.println();
     
      //5.通过Distpatch传递信息
      SOAPMessage responseMsg = dispatch.invoke(msg);
      responseMsg.writeTo(System.out);
      System.out.println();
     
      //6.将响应的消息转换为dom对象
      Document doc =  responseMsg.getSOAPBody().extractContentAsDocument();
      NodeList nl = doc.getElementsByTagName("user");
      JAXBContext ctx = JAXBContext.newInstance(User.class);
      for(int i=0;i<nl.getLength();i++) {
         Node n = nl.item(i);
         User u = (User)ctx.createUnmarshaller().unmarshal(n);
         System.out.println(u);
      }
   }

 相关的代码下载: http://download.csdn.net/detail/zhutulang/9487929     

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值