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

webservice WSDL java例子

发布时间:2020-12-16 22:18:55 所属栏目:安全 来源:网络整理
导读:http://blog.csdn.net/yaerfeng/article/details/8352307 WEBSERVICE快速入门的示例: 首先定义接口: [java] ? view plain ?copy ?print ? package ?com.whaty.platform.ws.server;?? ?? import ?javax.jws.WebService;?? /** ? ?*?@className:IMyservice.j

http://blog.csdn.net/yaerfeng/article/details/8352307


WEBSERVICE快速入门的示例:

首先定义接口:

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

其次编写实现类:

copy

?/**??
  • ?*?@className:MyServiceImpl.java?
  • ?*?@Desc:定义:SIB?service?implemention?bean?
  • ?*?@createTime:2012-12-21?上午01:01:22?
  • ?*/??
  • copy
    ?//endpointInterface指定接入点接口:接口必须存在??
  • @WebService(endpointInterface="com.whaty.platform.ws.server.IMyservice")??
  • class?MyServiceImpl?implements?IMyservice?{??
  • int?b)?{??
  • ????????System.out.println("a+b="+(a+b));??
  • ????????return?a+b;??
  • ????}??
  • ??
  • int?b)?{??
  • "a-b="+(a-b));??
  • return?a-b;??
  • ????}??
  • 最后发布我们的服务,直接右键运行main方法,如果控制台没报错,多半是发布成功了,否则检查你的代码:

    copy

    ?import?javax.xml.ws.Endpoint;??
  • ?*?@className:MyServer.java?
  • ?*?@Desc:发布服务?
  • ?*?@createTime:2012-12-21?上午01:02:39?
  • class?MyServer?{??
  • static?void?main(String[]?args)?{??
  • ????????//访问方式:http://localhost:7777/tudou?wsdl??
  • ????????String?address="http://localhost:7777/tudou";??
  • ????????Endpoint.publish(address,?new?MyServiceImpl());??
  • }??

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

    http://localhost:7777/tudou?wsdl

    浏览器显示如下:

    [html]? copy
    ??
      This?XML?file?does?not?appear?to?have?any?style?information?associated?with?it.?The?document?tree?is?shown?below.??
    1. <!--?
    2. ?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.??
    3. -->??
    4. <!--?
    5. ?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.??
    6. -->??
    7. <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">??
    8. types>??
    9. xsd:schemaxsd:import?namespace="http://server.ws.platform.whaty.com/"?schemaLocation="http://localhost:7777/tudou?xsd=1"/>??
    10. </message?name="minus"part?name="parameters"?element="tns:minus"message"minusResponse""tns:minusResponse"/>??
    11. "add""tns:add""addResponse""tns:addResponse"portType?name="IMyservice"operation?name=input?message=output?message=operationportTypebinding?name="MyServiceImplPortBinding"?type="tns:IMyservice"soap:binding?transport="http://schemas.xmlsoap.org/soap/http"?style="document"soap:operation?soapAction=""inputsoap:body?use="literal"outputbindingservice?name=port?name="MyServiceImplPort"?binding="tns:MyServiceImplPortBinding"soap:address?location="http://localhost:7777/tudou"portservicedefinitions>??

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

    copy

    ?package?com.whaty.platform.ws.client;??
  • import?java.net.MalformedURLException;??
  • import?java.net.URL;??
  • import?javax.xml.namespace.QName;??
  • import?javax.xml.ws.Service;??
  • import?com.whaty.platform.ws.server.IMyservice;??
  • ?*?@className:MyClient.java?
  • ?*?@Desc:访问发布的服务?
  • ?*?@createTime:2012-12-21?上午01:23:57?
  • class?MyClient?{??
  • try?{??
  • ????????????//服务WSDL?Document的地址??
  • ????????????URL?url?=?new?URL("http://localhost:7777/tudou?wsdl");??
  • //Qnameqname是qualified?name?的简写??
  • //2.构成:由名字空间(namespace)前缀(prefix)以及冒号(:),还有一个元素名称构成??
  • //由发布的wsdl可知namespace为http://server.ws.platform.whaty.com/,??
  • ????????????QName?qname=new?QName("http://server.ws.platform.whaty.com/","MyServiceImplService");??
  • ????????????Service?service=Service.create(url,?qname);??
  • ????????????IMyservice?ms=service.getPort(IMyservice.class);??
  • ????????????ms.add(1,?4);??
  • ????????????ms.minus(4);??
  • ????????}?catch?(MalformedURLException?e)?{??
  • ????????????e.printStackTrace();??
  • ????????}??

  • 控制台打印如下:

    a+b=5 a-b=-3

    (编辑:李大同)

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

    • 推荐文章
        热点阅读