- 创建一个Web Project
- 编写Web Service代码
- 启动Web Service服务
- 创建一个Java application project
- 编写客户端代码
- 客户端访问Web Service服务
创建一个Web Project
这里我选用Maven来创建,当然你也可以用其他方式来创建
mvn archetype:create -DgroupId=com.wstest -DartifactId=wstest -Dversion=1.0-SNAPSHOT -DarchetypeArtifactId=maven-archetype-webapp
创建好之后,我们给工程添加Spring 3.0 和 cxf 2.4.1的jar包的dependency,由于dependency信息在pom.xml中比较长,所以就不帖出来了,可以在附件中找到。
接着执行如下命令,以生成Eclipse工程配置文件
mvn -DdownloadSources=true eclipse:clean eclipse:eclipse -Dwtpversion=1.5
将工程导入Eclipse,我们就可以开始编码了。
编写Web Service代码
首先New一个对外暴露的接口HelloWorld.java
package com.wstest.service;
import javax.jws.WebService;
@WebService
public interface HelloWorld {
public String sayHello(String text);
}
接着创建一个接口的实现类HelloWorldImpl.java
package com.wstest.service.impl;
import javax.jws.WebService;
import com.wstest.service.HelloWorld;
@WebService
public class HelloWorldImpl implements HelloWorld {
@Override
public String sayHello(String text) {
return "Hello, I am Robot." + text;
}
}
最后我们创建一个MainServer类,用来启动我们的Web Service服务
package com.wstest.server;
import org.apache.cxf.endpoint.Server;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
import com.wstest.service.impl.HelloWorldImpl;
public class MainServer {
public static void main(String[] args) {
JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
factory.setServiceClass(HelloWorldImpl.class);
factory.setAddress("http://localhost:8080/HelloWorld");
// 注意别忘了添加service bean
HelloWorldImpl hello = new HelloWorldImpl();
factory.setServiceBean(hello);
Server server = factory.create();
server.start();
}
}
启动Web Service服务
执行MainServer启动Web Service服务,就可以打开浏览器查看wsdl信息了,输入http://localhost:8080/HelloWorld?wsdl
创建一个Java application project
mvn archetype:create -DgroupId=com.wsclient -DartifactId=wsclient -Dversion=1.0-SNAPSHOT -DarchetypeArtifactId=maven-archetype-quickstart
创建好之后,我们给工程添加Spring 3.0 和 cxf 2.4.1的jar包的dependency,dependency信息可以在附件中找到。
执行如下命令,以生成Eclipse工程配置文件
mvn -DdownloadSources=true eclipse:clean eclipse:eclipse
将工程导入Eclipse
编写客户端代码
创建一个Web Service暴露接口类,HelloWorld.java(类名,包名都一样)
package com.wstest.service;
import javax.jws.WebService;
@WebService
public interface HelloWorld {
public String sayHello(String text);
}
接着创建一个Client类
package com.wsclient.client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import com.wstest.service.HelloWorld;
public class Client {
public static void main(String[] args) {
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setAddress("http://localhost:8080/HelloWorld");
factory.setServiceClass(HelloWorld.class);
HelloWorld helloWorld = (HelloWorld) factory.create();
System.out.println(helloWorld.sayHello("How do you do."));
}
}
客户端访问Web Service服务
在Web Service服务已被启动的前提下,直接运行Client类,就好了。
输出结果
Hello, I am Robot.How do you do.
分享到:
相关推荐
CXF+spring+struts实现WebService
CXF2.6+SPRING3.0+STRUTS2.3整合的一个例子,含Jar包.
简单的webservice+Cxf+Spring数据对接实例以及jar.rar简单的webservice+Cxf+Spring数据对接实例以及jar.rar简单的webservice+Cxf+Spring数据对接实例以及jar.rar简单的webservice+Cxf+Spring数据对接实例以及jar.rar...
完整的cxf+hibernate+spring实现 weblogic 和tomcat上完美兼容
cxf+spring实现webservice
cxf+spring实现webservice的例子,在eclipse开发环境上测试通过,由于上传限制,没有传lib,只把代码上传了,有需要的可以给我留言,我给单发!
cxf+spring+client+springmvc
内容为CXF整合spring的服务端和客户端开发例子,注释不太详尽,网上的教程比较多,但是多跑不起来,特地把整个工程都打包给大家做参考,程序内容均来自网上整理,均可以运行。
cxf+spring+tomcat 只是演示,一个helloWorld的例子
忘记删除个人的资料了,现在重新传一个,供大家学习使用,互相交流,资源内容 为rar格式的 项目,cxf 为服务器端,cxfTest 为客户端,里边有很多调用方式,还有身份验证,使用时,搭配好 客户端和服务端的spring ...
cxf+spring实现webservice的客户端例子,在eclipse开发工具上已经测试通过,里面没有lib,如果有需要的话,可以给我留言,可以单发!
由于版本冲突包太多,整理的的很完整了例子(Spring+CXF)
代码是我一行行敲的,直接部署就能用,service,client端实现了:(cxf用的是3.0最新的) 1维数组, 2维数组, 3维数组, List, List , Map(adapter方式实现的), 直接返回bean, 返回object[], 做了header的安全认证校验.
cxf+spring整合,只要把项目导入到myeclipse中,就可以运行。
CXF+Spring+Tomcat发布WebService
这是我在使用cxf+spring进行开发中的一点总结,如何搭建开发环境、如何配置dao使用,如何解决乱码问题等的经验分享
这个是cxf 集成spring 的webservice的例子 供大家参考
cxf+spring开发所必备的jar包压缩包
web项目使用spring和cxf的一个开发实例,有简单的代码样例和jar。是一个完整的项目,最终发布完成时访问 http://ip:port/项目名称/webservices/ 就会发现你发布的webservice服务。