加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-16 22:18:38 所属栏目:安全 来源:网络整理
导读:新建一个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,St

新建一个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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读