cxf实现webservice

简介:

  Apache CXF 是一个开放源代码框架,提供了用于方便地构建和开发 Web 服务的可靠基础架构。它允许创建高性能和可扩展的服务,您可以将这样的服务部署在 Tomcat 和基于 Spring 的轻量级容器中,以及部署在更高级的服务器上,例如 Jboss、IBM WebSphere 或 BEA WebLogic。

简单介绍入门教程,也为自己记录下。

(1)下载cxf包,http://cxf.apache.org/download.html,我这里用的是2.4.0的包

        网盘地址如下:http://yun.baidu.com/share/link?shareid=564842495&uk=2836507213

        导入lib中的所有jar包,推荐使用library方式

(2)编写webservice接口类,接口实现类如下


接口需要指定annotation

1
2
3
4
5
6
@WebService
public  interface  IHello {
     public  String sayHi(String name);
     
     public  String printName(String name);
}


编写上述接口的实现类,annotation指定了endpointInterface与serviceName

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@WebService (endpointInterface= "com.xj.service.IHello" ,serviceName= "hello1Service" )
public  class  HelloImpl  implements  IHello{
 
     @Override
     public  String sayHi(String name) {
         System.out.println( "hi," +name);
         return  "hi," +name;
     }
 
     @Override
     public  String printName(String name) {
         System.out.println( "my name is," +name);
         return  "my name is," +name;
     }
 
}

  

(3)编写服务端,并启动

1
2
3
4
5
6
7
8
9
public  class  RunServer {
 
     public  static  void  main(String[] args) {
         IHello hello =  new  HelloImpl();
         Endpoint.publish( "http://localhost/cxf/hello" , hello);
         System.out.println( "启动server端" );
     }
 
}

此处同样采用的是endpoint来发布该服务,当然也可以使用JaxWsServerFactoryBean

运行main方法,访问http://localhost/cxf/hello?wsdl 可以看到该服务的wsdl文件

(4)编写客户端

1
2
3
4
5
6
7
8
9
10
11
12
public  class  RunClient {
 
     public  static  void  main(String[] args) {
         JaxWsProxyFactoryBean proxy =  new  JaxWsProxyFactoryBean();
         proxy.setServiceClass(IHello. class );
         proxy.setAddress( "http://localhost/cxf/hello?wsdl" );
         IHello hello = (IHello)proxy.create();
         System.out.println(hello.sayHi( "xiejun" ));
         System.out.println(hello.printName( "xiexie" ));
     }
 
}

使用JaxWsProxyFactoryBean创建代理,指定service类,指定wsdl地址,

 调用代理类的create方法,即可访问所有方法




     本文转自布拉君君 51CTO博客,原文链接:http://blog.51cto.com/5148737/1606249,如需转载请自行联系原作者


相关文章
【Azure 应用服务】Web App Service 中的 应用程序配置(Application Setting) 怎么获取key vault中的值
【Azure 应用服务】Web App Service 中的 应用程序配置(Application Setting) 怎么获取key vault中的值
132 0
|
10月前
【Azure App Service】PowerShell脚本批量添加IP地址到Web App允许访问IP列表中
Web App取消公网访问后,只允许特定IP能访问Web App。需要写一下段PowerShell脚本,批量添加IP到Web App的允许访问IP列表里!
147 2
|
关系型数据库 MySQL Linux
【Azure 应用服务】在创建Web App Service的时候,选Linux系统后无法使用Mysql in App
【Azure 应用服务】在创建Web App Service的时候,选Linux系统后无法使用Mysql in App
【Azure 应用服务】在创建Web App Service的时候,选Linux系统后无法使用Mysql in App
|
缓存 JavaScript 前端开发
Web Workers与Service Workers:后台处理与离线缓存
Web Workers 和 Service Workers 是两种在Web开发中处理后台任务和离线缓存的重要技术。它们在工作原理和用途上有显著区别。
193 1
|
Shell PHP Windows
【Azure App Service】Web Job 报错 UNC paths are not supported. Defaulting to Windows directory.
【Azure App Service】Web Job 报错 UNC paths are not supported. Defaulting to Windows directory.
109 0
|
Linux 应用服务中间件 网络安全
【Azure 应用服务】查看App Service for Linux上部署PHP 7.4 和 8.0时,所使用的WEB服务器是什么?
【Azure 应用服务】查看App Service for Linux上部署PHP 7.4 和 8.0时,所使用的WEB服务器是什么?
108 0
【Azure 应用服务】通过 Web.config 开启 dotnet 应用的 stdoutLog 日志,查看App Service 产生500错误的原因
【Azure 应用服务】通过 Web.config 开启 dotnet 应用的 stdoutLog 日志,查看App Service 产生500错误的原因
202 0
|
Linux Python
【Azure 应用服务】Azure App Service For Linux 上实现 Python Flask Web Socket 项目 Http/Https
【Azure 应用服务】Azure App Service For Linux 上实现 Python Flask Web Socket 项目 Http/Https
167 0
|
存储 安全 网络安全
【Azure 环境】使用Azure中的App Service部署Web应用,以Windows为主机系统是否可以启动防病毒,防恶意软件服务呢(Microsoft Antimalware)?
【Azure 环境】使用Azure中的App Service部署Web应用,以Windows为主机系统是否可以启动防病毒,防恶意软件服务呢(Microsoft Antimalware)?
127 0
|
存储 Linux 网络安全
【Azure 应用服务】App Service For Linux 如何在 Web 应用实例上住抓取网络日志
【Azure 应用服务】App Service For Linux 如何在 Web 应用实例上住抓取网络日志
101 0

热门文章

最新文章