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

webservice快速入门-使用JAX-WS注解的方式快速搭建ws服务端和客

发布时间:2020-12-16 23:03:04 所属栏目:安全 来源:网络整理
导读:?? ??? WEBSERVICE快速入门的示例: 首先定义接口: [java] view plain copy print ? package ?com.whaty.platform.ws.server;?? ?? import ?javax.jws.WebService;?? ?? /** ? ?*?@className:IMyservice.java ? ?*?@Desc:定义:SEI?service?endpoint?interf
??

???

WEBSERVICE快速入门的示例:

首先定义接口:

[java] view plain copy print ?
  1. package?com.whaty.platform.ws.server;??
  2. ??
  3. import?javax.jws.WebService;??
  4. ??
  5. /**?
  6. ?*?@className:IMyservice.java?
  7. ?*?@Desc:定义:SEI?service?endpoint?interface?
  8. ?*?@author:lizhuang?
  9. ?*?@createTime:2012-12-21?上午12:57:18?
  10. ?*/??
  11. //JAX-WS注解,表示java?api?xml?for?webservice。JDK自带API的XML格式的webservice??
  12. @WebService??
  13. public?interface?IMyservice?{??
  14. ??????
  15. ????int?add(int?a,?int?b);??
  16. ??
  17. ????int?minus(int?a,?int?b);??
  18. }??

其次编写实现类:

[java] view plain copy print ?
  1. package?com.whaty.platform.ws.server;??
  2. ??
  3. import?javax.jws.WebService;??
  4. ??
  5. /**??
  6. ?*?@className:MyServiceImpl.java?
  7. ?*?@Desc:定义:SIB?service?implemention?bean?
  8. ?*?@author:lizhuang?
  9. ?*?@createTime:2012-12-21?上午01:01:22?
  10. ?*/??
[java] view plain copy print ?
  1. //endpointInterface指定接入点接口:接口必须存在??
  2. @WebService(endpointInterface="com.whaty.platform.ws.server.IMyservice")??
  3. public?class?MyServiceImpl?implements?IMyservice?{??
  4. ??
  5. ????public?int?add(int?a,?int?b)?{??
  6. ????????System.out.println("a+b="+(a+b));??
  7. ????????return?a+b;??
  8. ????}??
  9. ??
  10. ????public?int?minus(int?a,?int?b)?{??
  11. ????????System.out.println("a-b="+(a-b));??
  12. ????????return?a-b;??
  13. ????}??
  14. ??
  15. }??

最后发布我们的服务,直接右键运行main方法,如果控制台没报错,多半是发布成功了,否则检查你的代码:

[java] view plain copy print ?
  1. package?com.whaty.platform.ws.server;??
  2. ??
  3. import?javax.xml.ws.Endpoint;??
  4. ??
  5. /**?
  6. ?*?@className:MyServer.java?
  7. ?*?@Desc:发布服务?
  8. ?*?@author:lizhuang?
  9. ?*?@createTime:2012-12-21?上午01:02:39?
  10. ?*/??
  11. public?class?MyServer?{??
  12. ????public?static?void?main(String[]?args)?{??
  13. ????????//访问方式:http://localhost:7777/tudou?wsdl??
  14. ????????String?address="http://localhost:7777/tudou";??
  15. ????????Endpoint.publish(address,?new?MyServiceImpl());??
  16. ????}??
  17. }??


浏览器地址栏输入:访问webservice看看是否发布成功【地址后面加上"?wsdl"】:

http://localhost:7777/tudou?wsdl


浏览器显示如下:

[html] view plain copy print ?
  1. This?XML?file?does?not?appear?to?have?any?style?information?associated?with?it.?The?document?tree?is?shown?below.??
  2. <!--?
  3. ?Published?by?JAX-WS?RI?at?http://jax-ws.dev.java.net.?RI's?version?is?JAX-WS?RI?2.1.6?in?JDK?6.??
  4. -->??
  5. <!--?
  6. ?Generated?by?JAX-WS?RI?at?http://jax-ws.dev.java.net.?RI's?version?is?JAX-WS?RI?2.1.6?in?JDK?6.??
  7. -->??
  8. <definitions?xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"?xmlns:tns="http://server.ws.platform.whaty.com/"?xmlns:xsd="http://www.w3.org/2001/XMLSchema"?xmlns="http://schemas.xmlsoap.org/wsdl/"?targetNamespace="http://server.ws.platform.whaty.com/"?name="MyServiceImplService">??
  9. <types>??
  10. <xsd:schema>??
  11. <xsd:import?namespace="http://server.ws.platform.whaty.com/"?schemaLocation="http://localhost:7777/tudou?xsd=1"/>??
  12. </xsd:schema>??
  13. </types>??
  14. <message?name="minus">??
  15. <part?name="parameters"?element="tns:minus"/>??
  16. </message>??
  17. <message?name="minusResponse">??
  18. <part?name="parameters"?element="tns:minusResponse"/>??
  19. </message>??
  20. <message?name="add">??
  21. <part?name="parameters"?element="tns:add"/>??
  22. </message>??
  23. <message?name="addResponse">??
  24. <part?name="parameters"?element="tns:addResponse"/>??
  25. </message>??
  26. <portType?name="IMyservice">??
  27. <operation?name="minus">??
  28. <input?message="tns:minus"/>??
  29. <output?message="tns:minusResponse"/>??
  30. </operation>??
  31. <operation?name="add">??
  32. <input?message="tns:add"/>??
  33. <output?message="tns:addResponse"/>??
  34. </operation>??
  35. </portType>??
  36. <binding?name="MyServiceImplPortBinding"?type="tns:IMyservice">??
  37. <soap:binding?transport="http://schemas.xmlsoap.org/soap/http"?style="document"/>??
  38. <operation?name="minus">??
  39. <soap:operation?soapAction=""/>??
  40. <input>??
  41. <soap:body?use="literal"/>??
  42. </input>??
  43. <output>??
  44. <soap:body?use="literal"/>??
  45. </output>??
  46. </operation>??
  47. <operation?name="add">??
  48. <soap:operation?soapAction=""/>??
  49. <input>??
  50. <soap:body?use="literal"/>??
  51. </input>??
  52. <output>??
  53. <soap:body?use="literal"/>??
  54. </output>??
  55. </operation>??
  56. </binding>??
  57. <service?name="MyServiceImplService">??
  58. <port?name="MyServiceImplPort"?binding="tns:MyServiceImplPortBinding">??
  59. <soap:address?location="http://localhost:7777/tudou"/>??
  60. </port>??
  61. </service>??
  62. </definitions>??


下面我们创建客户端访问:

[java] view plain copy print ?
  1. package?com.whaty.platform.ws.client;??
  2. ??
  3. import?java.net.MalformedURLException;??
  4. import?java.net.URL;??
  5. ??
  6. import?javax.xml.namespace.QName;??
  7. import?javax.xml.ws.Service;??
  8. ??
  9. import?com.whaty.platform.ws.server.IMyservice;??
  10. ??
  11. /**?
  12. ?*?@className:MyClient.java?
  13. ?*?@Desc:访问发布的服务?
  14. ?*?@author:lizhuang?
  15. ?*?@createTime:2012-12-21?上午01:23:57?
  16. ?*/??
  17. public?class?MyClient?{??
  18. ????public?static?void?main(String[]?args)?{??
  19. ??
  20. ????????try?{??
  21. ????????????//服务WSDL?Document的地址??
  22. ????????????URL?url?=?new?URL("http://localhost:7777/tudou?wsdl");??
  23. ????????????//Qnameqname是qualified?name?的简写??
  24. ????????????//2.构成:由名字空间(namespace)前缀(prefix)以及冒号(:),还有一个元素名称构成??
  25. ????????????//由发布的wsdl可知namespace为http://server.ws.platform.whaty.com/,??
  26. ????????????QName?qname=new?QName("http://server.ws.platform.whaty.com/","MyServiceImplService");??
  27. ????????????Service?service=Service.create(url,?qname);??
  28. ????????????IMyservice?ms=service.getPort(IMyservice.class);??
  29. ????????????ms.add(1,?4);??
  30. ????????????ms.minus(1,?4);??
  31. ????????}?catch?(MalformedURLException?e)?{??
  32. ????????????e.printStackTrace();??
  33. ????????}??
  34. ????}??
  35. }??


控制台打印如下:

a+b=5 a-b=-3

(编辑:李大同)

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

    推荐文章
      热点阅读