`
devon.k
  • 浏览: 88414 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

CXF + Spring 3.0的学习例子

阅读更多
  • 创建一个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.
  • ws.rar (8.2 KB)
  • 下载次数: 166
分享到:
评论
3 楼 一炮送你回车库 2013-01-15  
你这都没spring,那个注解也不是spring的
2 楼 焦志广 2013-01-10  
哪有spring啊
1 楼 humaniam 2012-08-08  
多谢你的例子,帮大忙了

相关推荐

Global site tag (gtag.js) - Google Analytics