当前位置 : 主页 > 网页制作 > Nodejs >

webservice 入门笔记三通过payload发送消息

来源:互联网 收集:自由互联 发布时间:2021-06-24
新建一个User类: packagecom.zhutulang.soap; importjavax.xml.bind.annotation.XmlRootElement;@XmlRootElementpublic class User { private String name; private String pwd; private int age; public User() { super(); } public User(String name

新建一个User类:

 

packagecom.zhutulang.soap;
 
importjavax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class User {
 
   private String name;
   private String pwd;
   private int age;
   public User() {
      super();
   }
   public User(String name, String pwd, int age) {
      super();
      this.name = name;
      this.pwd = pwd;
      this.age = age;
   }
 
   @Override
   public String toString() {
      return "[name:"+name+",pwd:"+pwd+",age:"+age+"]";
   }
   public String getName() {
      return name;
   }
 
   public void setName(String name) {
      this.name = name;
   }
   public String getPwd() {
      return pwd;
   }
   public void setPwd(String pwd) {
      this.pwd = pwd;
   }
   public int getAge() {
      return age;
   }
   public void setAge(int age) {
      this.age = age;
   }
}


然后在接口中新增一个方法:

@WebResult(name="user")
   public User addUser(@WebParam(name="user")User user);
 


接口实现很简单,就是把一个user加入list中

packagecom.zhutulang.soap;
 
importjava.util.ArrayList;
importjava.util.List;
 
importjavax.jws.WebService;
 
@WebService(endpointInterface="com.zhutulang.soap.MyServiceInter")
public class MyServiceInterImpl implements MyServiceInter {
 
   private List<User> users = new ArrayList<User>();
  
   @Override
   public int add(int a, int b) {
      System.out.println("a+b"+(a+b));
      return a+b;
   }
 
   @Override
   public User addUser(User user) {
      users.add(user);
      return user;
   }
}
Test方法:
   /**
    * <p>Title: 通过payload发送消息</p>
    * <p>Description: </p>
    * @throws MalformedURLException
    * @throws JAXBException
    * @throwsTransformerFactoryConfigurationError
    * @throws TransformerException
    * @throws XPathExpressionException
    * @author zhutulang
    * @version 1.0
    */
   @Test
   public void test3() throws MalformedURLException, JAXBException,TransformerFactoryConfigurationError, TransformerException,XPathExpressionException{
      //1.创建服务
      URL url = new URL(wsdlUrl);
      QName qName = new QName(namespaceUrl, "MyServiceInterImplService");
      Service service = Service.create(url, qName);
     
      //2.创建dispatch (通过源数据方式传递)
      Dispatch<Source> dispatch = service.createDispatch(new QName(namespaceUrl,
            "MyServiceInterImplPort"), Source.class, Service.Mode.PAYLOAD);
     
      //3.根据一个User对象创建相应的xml
      User user = new User("Lucy", "123456", 26);
      JAXBContext ctx = JAXBContext.newInstance(User.class);
      Marshaller mar = ctx.createMarshaller();
      mar.setProperty(Marshaller.JAXB_FRAGMENT, true);
      StringWriter writer = new StringWriter();
      mar.marshal(user, writer);
     
      //4、封装相应的part addUser
      String payload = "<nn:addUser xmlns:nn=\""+namespaceUrl+"\">"+writer.toString()+"</nn:addUser>";
      System.out.println(payload);
      StreamSource rs = new StreamSource(new StringReader(payload));
     
      //5.通过dispa传递payload
      Source response = dispatch.invoke(rs);
     
      //6.将source转换为DOM
      Transformer transformer = TransformerFactory.newInstance().newTransformer();
      DOMResult result = new DOMResult();
      transformer.transform(response, result);
     
      //7.处理相应的消息
      XPath xPath = XPathFactory.newInstance().newXPath();
      NodeList nl = (NodeList) xPath.evaluate("//user", result.getNode(), XPathConstants.NODESET);
      User ru = (User) ctx.createUnmarshaller().unmarshal(nl.item(0));
      System.out.println(ru);
   }

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