webservice 入门笔记三通过payload发送消息
来源:互联网 发布:sql注入病毒的原理 编辑:程序博客网 时间:2024/04/28 12:40
新建一个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, 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
0 0
- webservice 入门笔记三通过payload发送消息
- webservice 入门笔记四发送header信息
- webservice入门笔记二soap消息的处理
- webservice向指定socket端口发送消息
- HttpURLConnection 直接发送soap消息调用webservice
- HttpURLConnection 直接发送soap消息调用webservice
- HttpURLConnection 直接发送soap消息调用webservice
- HttpURLConnection 直接发送soap消息调用webservice
- WebService入门学习笔记
- webservice入门级笔记
- webservice 入门笔记一
- postman 模拟 payload(body)发送
- 笔记 android发送状态栏消息
- unix学习笔记------消息队列---发送消息
- Web Service学习笔记(webservice、soap、wsdl、jws详细分析) Webservice的wsdl文件解析与Soap消息的发送、接收(不生成java客户端代码)
- webService笔记(2)---入门示例
- QuickFIX/N入门: 二、发送消息及接收消息
- QuickFIX/N入门:二、发送消息及接收消息
- 虚继承
- PHP 运行方式(PHP SAPI介绍)
- 屏蔽Adobe连接验证服务器
- 安卓数据库编程总结(1)
- Host 'heChengjian' is not allowed to connect to this MySQL server
- webservice 入门笔记三通过payload发送消息
- 自定义一个图片在上,文字在下的按钮
- 《机器学习实战》小结
- 数据结构 - 2顺序表及链表基本运算实现
- Source Insight阅读Matlab源码设置流程(其他语言也通用)
- 吴恩达机器学习笔记_第二周
- getaddrinfo函数
- 自定义View(一)
- android studio出现非法字符的解决办法