webservice入门笔记二soap消息的处理

Soap消息的处理

首先,复习下,创建一个webservice服务。新建一个java工程 Soap。接口、接口、服务类分别如下:

packagecom.zhutulang.soap;
 
importjavax.jws.WebParam;
importjavax.jws.WebResult;
importjavax.jws.WebService;
 
@WebService
public interface MyServiceInter {
 
   @WebResult(name="addResult")
   public int add(@WebParam(name="a")int a, @WebParam(name="b")int b);
}

packagecom.zhutulang.soap;
 
importjavax.jws.WebService;
 
@WebService(endpointInterface="com.zhutulang.soap.MyServiceInter")
public class MyServiceInterImpl implements MyServiceInter {
 
   @Override
   public int add(int a, int b) {
      System.out.println("a+b"+(a+b));
      return a+b;
   }
}

packagecom.zhutulang.soap;
 
importjavax.xml.ws.Endpoint;
 
public class MyServer {
 
   public static void main(String[] args) {
      Endpoint.publish("http://localhost:9999/webservice", new MyServiceInterImpl());
      System.out.println("publish success");
   }
}


然后,我们新建一个SoapTest类,首先来看看怎样封装soap消息:

packagecom.zhutulang.soap;
 
importjava.io.IOException;
 
importjavax.xml.namespace.QName;
importjavax.xml.soap.MessageFactory;
importjavax.xml.soap.SOAPBody;
importjavax.xml.soap.SOAPBodyElement;
importjavax.xml.soap.SOAPEnvelope;
importjavax.xml.soap.SOAPException;
importjavax.xml.soap.SOAPMessage;
importjavax.xml.soap.SOAPPart;
 
importorg.junit.Test;
 
public class SoapTest {
 
   @Test
   public void test1() throws SOAPException, IOException{
      MessageFactory factory = MessageFactory.newInstance();
      SOAPMessage message = factory.createMessage();
      SOAPPart part = message.getSOAPPart();
      SOAPEnvelope envelope = part.getEnvelope();
      SOAPBody body = envelope.getBody();
      QName qName = new QName("http://soap.zhutulang.com/", "add", "ns");
      SOAPBodyElement element = body.addBodyElement(qName);
      element.addChildElement("a").setValue("120");
      element.addChildElement("b").setValue("220");
      message.writeTo(System.out);
   }
}


其中,QName(String namespaceURI,String localPart,String prefix)各参数含义是          命名空间 URI、本地部分和前缀的 QName。

 

下面是一个发送soap消息并接受返回消息的方法示例:

   /**
    * <p>Title: test2</p>
    * <p>Description:发送接收soap消息 </p>
    * @author zhutulang
    * @version 1.0
    * @throws SOAPException
    * @throws IOException
    */
   @Test
   public void test2() throws SOAPException, IOException{
      //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();
     
      //4.创建QName来指定消息中传递的数据
      QName ename = new QName(namespaceUrl, "add", "ns");
      SOAPBodyElement element = body.addBodyElement(ename);
      element.addChildElement("a").setValue("22");
      element.addChildElement("b").setValue("33");
     
      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.getSOAPPart().getEnvelope().getBody().extractContentAsDocument();
      String addResult = doc.getElementsByTagName("addResult").item(0).getTextContent();
      System.out.println("addResult="+addResult);
   }


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


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值