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

webservice-demo

发布时间:2020-12-16 23:21:04 所属栏目:安全 来源:网络整理
导读:webservice开发步骤: ? 服务端开发 —定义服务接口 —实现服务接口 —通过CXF、xfire、axis等框架对外发布服务 ?客户端调用 —通过有效途径(双方沟通、UDDI查找等)获取到Webservice的信息 —通过wsdl生成客户端(或者服务提供方提供对应的接口jar包) —

webservice开发步骤:

? 服务端开发

—定义服务接口

—实现服务接口

—通过CXF、xfire、axis等框架对外发布服务

?客户端调用

—通过有效途径(双方沟通、UDDI查找等)获取到Webservice的信息

—通过wsdl生成客户端(或者服务提供方提供对应的接口jar包)

—调用服务

?

?

service接口:

 
 
  1. package?com.ws.jaxws.sayHello.service; ?
  2. ?
  3. import?javax.jws.WebService; ?
  4. import?javax.jws.soap.SOAPBinding; ?
  5. import?javax.jws.soap.SOAPBinding.Style; ?
  6. ?
  7. @WebService ?
  8. //@SOAPBinding(style=Style.RPC) ?
  9. public?interface?SayHelloService?{ ?
  10. ????public?String?sayHello(String?name); ?
  11. } ?

service接口的实现类:

  • package?com.ws.jaxws.sayHello.service.impl; ?
  • ?
  • import?javax.jws.WebService; ?
  • ?
  • import?com.ws.jaxws.sayHello.service.SayHelloService; ?
  • ?
  • @WebService(endpointInterface?=?"com.ws.jaxws.sayHello.service.SayHelloService",?name?=?"sayHelloService",?targetNamespace?=?"sayHello") ?
  • ?
  • public?class?SayHelloServiceImpl?implements?SayHelloService?{ ?
  • ?
  • ????@Override ?
  • ????public?String?sayHello(String?name)?{ ?
  • ????????//?TODO?Auto-generated?method?stub ?
  • ????????if("".equals(name)||name==null){ ?
  • ????????????name="nobody"; ?
  • ????????} ?
  • ????????return?"hello?"+name; ?
  • ????} ?
  • ?
  • }?
  • 客户端:

  • package?com.ws.jaxws.sayHello.server; ?
  • ?
  • import?javax.xml.ws.Endpoint; ?
  • ?
  • import?com.ws.jaxws.sayHello.service.impl.SayHelloServiceImpl; ?
  • ?
  • public?class?SayHelloServer?{ ?
  • ????public?static?void?main(String[]?args)?throws?Throwable{ ?
  • ????????Endpoint.publish("http://localhost:8888/sayHelloService",?new?SayHelloServiceImpl()); ?
  • ????????System.out.println("SayHelloService?is?running...."); ?
  • ????????Thread.sleep(5?*?60?*?1000); ?
  • ????????System.out.println("time?out...."); ?
  • ????????System.exit(0); ?
  • ????} ?
  • }?
  • 运行客户端代码后在浏览器输入http://localhost:8888/sayHelloServoce?wsdl,即可生成wsdl代码,如下所示:

    客户端调用:

  • package?com.ws.jaxws.sayHello.client; ?
  • ?
  • import?java.net.MalformedURLException; ?
  • import?java.net.URL; ?
  • ?
  • import?javax.xml.namespace.QName; ?
  • import?javax.xml.ws.Service; ?
  • ?
  • import?com.ws.jaxws.sayHello.service.SayHelloService; ?
  • ?
  • public?class?SayHelloClient?{ ?
  • ????public?static?void?main(String[]?args)?throws?MalformedURLException?{ ?
  • ????????String?url="http://localhost:8888/sayHelloService?wsdl"; ?
  • ????????Service?service=Service.create(new?url),?new?QName("sayHello","SayHelloServiceImplService")); //获得webservice的信息 ?
  • ????????SayHelloService?sayHelloService=service.getPort(SayHelloService.class);//return?a?proxy ?
  • ????????System.out.println(sayHelloService.sayHello(null));//调用服务 ?
  • ????????System.out.println(sayHelloService.sayHello("zhangsan")); ?
  • ????} ?
  • }?
  • 运行客户端代码,控制端输出: hello nobody hello zhangsan

    (编辑:李大同)

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

      推荐文章
        热点阅读