Apache CXF实现WebService发布和调用
第一种方法:不用导入cxf jars
服务端:
1、 新建Web工程
2、新建接口和实现类、测试类
目录结构图如
下:

接口代码:
package com.cxf.spring.service; import javax.jws.WebMethod;
import javax.jws.WebService; @WebService
public interface IGreetingService { @WebMethod
public String greeting(String name); }
实现类代码:
package com.cxf.spring.service; import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService; @WebService
public class GreetingServiceImpl implements IGreetingService{ @WebMethod
@Override
public String greeting(@WebParam(name ="name") String name) { return "HI:....."+name;
} }
测试类代码:
package com.cxf.spring.test;
import javax.xml.ws.Endpoint;
import com.cxf.spring.service.GreetingServiceImpl;
public class TestSpring {
public static void main(String[] args) {
pushWS_1();
}
public static void pushWS_1() {
String address = "http://localhost:8100/greeting";
System.out.println("Server start....");
Endpoint.publish(address, new GreetingServiceImpl());
System.out.println("Server ready....");
try {
Thread.sleep(3*60*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Server exit");
System.exit(0);
}
}
运行测试类:访问:http://localhost:8100/greeting?wsdl

客户端:
1、新建java工程 ,配置CXF环境变量 (下载Apache CXF2.7 )
2、CMD打开命令窗口,运行以下命令,生产客户端代码: wsdl2java.bat -p com.cxf.spting -client -encoding utf-8 -noAddressBinding http://localhost:8100/greeting?wsdl
拷贝到新建java工程的src文件下


运行GreetingServiceImpl_GreetingServiceImplPort_Client.java访问webservice

第二种:
新建web工程 引入cxf依赖包(最小jar)

修改以上测试类代码
package com.cxf.spring.test; import javax.xml.ws.Endpoint; import org.apache.cxf.jaxws.JaxWsServerFactoryBean; import com.cxf.spring.service.GreetingServiceImpl;
import com.cxf.spring.service.IGreetingService; public class TestSpring { public static void main(String[] args) {
pushWS_2();
} public static void pushWS_2() { JaxWsServerFactoryBean bean = new JaxWsServerFactoryBean();
bean.setAddress("http://localhost:8100/greeting");
bean.setServiceClass(IGreetingService.class);
bean.setServiceBean(new GreetingServiceImpl());
bean.create(); System.out.println("Server ready...."); try {
Thread.sleep(3*60*1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Server exit");
System.exit(0);
}
运行访问
客户端:按照第一种方法执行。。。
另外两种调用webservice的方法
新建工程 ------测试类 ----- 接口:
package com.cxf.test; import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory; import com.cxf.test.client.IGreetingService; public class TestClient_2 { public static void main(String[] args) { pull_1(); pull_2();
} public static void pull_1(){
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient("http://localhost:8100/greeting?wsdl");
try {
Object[] objs = client.invoke("greeting", "张三");
System.out.println(objs[0].toString());
} catch (Exception e) {
e.printStackTrace();
}
} public static void pull_2() { JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(IGreetingService.class);
factory.setAddress("http://localhost:8100/greeting?wsdl");
IGreetingService service = (IGreetingService) factory.create();
System.out.println(">>>>>>>>Client: " + service.greeting("战士")); } }
package com.cxf.test.client; import javax.jws.WebMethod;
import javax.jws.WebService; @WebService(targetNamespace="http://service.spring.cxf.com/", name = "IGreetingService")
public interface IGreetingService { @WebMethod
public String greeting(String name); }
Apache CXF实现WebService发布和调用的更多相关文章
- Apache CXF实现WebService入门教程(附完整源码)
Apache CXF实现WebService非常简单实用,只需要几步就可以实现一个简单的web service. 首先我们需要新建一个maven项目,在pom中添加依赖和jetty作为测试的web s ...
- Apache CXF使用Jetty发布WebService
一.概述 Apache CXF提供了用于方便地构建和开发WebService的可靠基础架构.它允许创建高性能和可扩展的服务,可以部署在Tomcat和基于Spring的轻量级容器中,也可以部署在更高级的 ...
- 使用apache cxf实现webservice服务
1.在idea中使用maven新建web工程并引入spring mvc,具体可以参考https://www.cnblogs.com/laoxia/p/9311442.html; 2.在工程POM文件中 ...
- 使用axis2进行webservice发布与调用
一.介绍下cxf.axis.axis2区别 新一代的Web Services 框架如Axis2.CXF 都是由现有的项目中逐渐演化而来的,Axis2 是由大家熟悉的Axis 1.x 系列演化过来,而A ...
- axis2 webservice 发布、调用与项目集成
发布 1.在apache官网下载axis2包,下载Binary Distribution和War Distribution两个zip. 2.将war放入tomcat webapps下部署.并输入 ht ...
- Apache cxf暴露接口以及客户端调用之WebService初步理解
在我们真实的项目中,经常会调用别人提供给我们的接口,或者在自己的团队中, restful风格的前后端分离也经常会提供一个后端接口暴露出去供app,或者.net/C/C++程序员去调用,此时就需要使用到 ...
- webService发布和调用--Axis2
一.工具 1.下载 Axis2以及eclipse的Axis2插件.http://axis.apache.org/axis2/java/core/download.cgi 2.axis2-1.7.1-w ...
- springmvc 集成apache cxf 开发webservice 示例
今天需要在springmvc中增加webservice功能,试了多次axis2,和cxf都不行,后来发现在springmvc中最好用cxf集成非常方便,在又一次尝试后终于把demo整合到现有的项目中 ...
- WebService发布与调用问题:expected: {http://schemas.xmlsoap.org/soap/envelope/}Envelope but found: {http://schemas.xmlsoap.org/wsdl/}definitions
Mailbox===AsYVzdwoY_b6uD s>>>>>>>javax.xml.ws.Service@103bf65 hs>>>> ...
随机推荐
- STM32之USART-RS485
转载自:http://www.cnblogs.com/itloverhpu/p/3278014.html 1.今天调试HDMI8X8背板和板卡的通信,一直有问题:背板可以和PC正常通信,背板可以发命令 ...
- 怎么找到占用usb的模块,linux下Jlink连接失败
问题是这样产生的,我在linux下安装jlink,启动JLinkExe执行,总是提示不能通过usb连接: SEGGER J-Link Commander V5.10q (Compiled Mar :: ...
- 使用中国版 Office 365 -- Team Site分享
Team Site(工作组网站)主要用于团队内部的协同工作,团队(组织机构)内部每个需要使用Team Site的用户都需要一个Office 365的license.但是如果我们需要将Team Site ...
- 跟我一起写 Makefile
转自 陈皓 的博客:http://blog.csdn.net/haoel/article/details/2886 1. 概述 2. 关于程序的编译和链接 3. Makefile 介绍 4. Make ...
- Java开发之JSP指令
一.page指令 page指令是最常用的指令,用来说明JSP页面的属性等.JSP指令的多个属性可以写在一个page指令里,也可以写在多个指令里.但需要注意的是,无论在哪个page指令里的属性,任何pa ...
- AC日记——基因相关性 openjudge 1.7 03
03:基因相关性 总时间限制: 1000ms 内存限制: 65536kB 描述 为了获知基因序列在功能和结构上的相似性,经常需要将几条不同序列的DNA进行比对,以判断该比对的DNA是否具有相关性 ...
- Stunnel服务端
Stunnel on Debian GNU/Linux 6 (squeeze) 传统的POP3, SMTP, Samba等服务,都是不加密的协议(即在网络上明文传输数据),通过stunnel,可以将访 ...
- HTML5网站如何做到完全不需要jQuery
jQuery是现在最流行的JavaScript工具库. 据统计,目前全世界57.3%的网站使用它.也就是说,10个网站里面,有6个使用jQuery.如果只考察使用工具库的网站,这个比例就会上升到惊人的 ...
- QFontMetrics的一个问题
今天发现QFontMetric的一个问题 QFontMetrics可以输入一个字体和一个字符串,计算出字符串在这种字体下的显示尺寸. 这种计算比较耗时间,因此Qt内部应该是缓存了计算结果.具体方法是在 ...
- Debian 8(Jessie) 安装自带Mysql
执行命令 sudo apt-get install mysql-server 这会把mysql-client也装上, 版本都是5.5. 安装过程中会提示你输入两遍root口令. 用ps aux|gre ...