/**
**QQ:252574345
**MSN:[email protected]
*/
使用CXF Simple FrontEnd Project,不需要在业务接口中设置具体的协议、@Web Service、@Web Method,仅仅需要创建与具体webservice技术无关的业务接口就可以了(真有点像SCA了,呵呵不恰当的对比)。利用代理类进行发布服务和获取服务
(工程文件附后下载)
1。业务接口
package demo.zhaoyl.server;
/**
* 业务接口
* 与wsdl first project 、java first project方式不同的是
* 这个业务接口仅仅是普通的java interface
* @author zhaoyl
*
*/
public interface HelloWorld
{
String sayHi(String name);
}
2。业务接口实现类
package demo.zhaoyl.server;
/**
* 业务接口实现类
* @author zhaoyl
*
*/
public class HelloWorldImpl implements HelloWorld {
/**
* 向客户端返回一个名字
*/
public String sayHi(String name) {
return "Hello " + name;
}
}
3。服务端
package demo.zhaoyl.server;
import org.apache.cxf.frontend.ServerFactoryBean;
/**
* 服务端
* @author zhaoyl
*
*/
public class Server {
protected Server() throws Exception
{
HelloWorldImpl helloworldImpl = new HelloWorldImpl();
ServerFactoryBean svrFactory = new ServerFactoryBean();
//设置服务接口类
svrFactory.setServiceClass(HelloWorld.class);
//address 也就是wsdl文件中的 <soap:address location="http://localhost:9000/Hello" />
svrFactory.setAddress("http://localhost:9000/Hello");
//在浏览器端通过http://localhost:9000/Hello?wsdl访问生成的wsdl文件
//设置服务实现接口类
svrFactory.setServiceBean(helloworldImpl);
//创建服务
svrFactory.create();
}
public static void main(String args[]) throws Exception {
new Server();
System.out.println("Server ready...");
Thread.sleep(5 * 60 * 1000);
System.out.println("Server exiting");
System.exit(0);
}
}
4。服务端
package demo.zhaoyl.client;
import demo.zhaoyl.server.HelloWorld;
import org.apache.cxf.frontend.ClientProxyFactoryBean;
/**
* 客户端
* 仅仅需要一个与具体技术无关的java接口HelloWorld
* @author zhaoyl
*
*/
public final class Client {
private Client()
{
}
public static void main(String args[]) throws Exception
{
ClientProxyFactoryBean factory = new ClientProxyFactoryBean();
//设置已发布服务接口
factory.setServiceClass(HelloWorld.class);
//为客户端代理bean 设置已经发布的服务地址
factory.setAddress("http://localhost:9000/Hello");
//获取已发布服务接口实例
HelloWorld client = (HelloWorld)factory.create();
System.out.println("Invoke 远程服务接口方法sayHi()....");
//返回客户机器用户名称
System.out.println(client.sayHi(System.getProperty("user.name")));
System.exit(0);
}
}
启动服务
运行客户端
结果:
Invoke 远程服务接口方法sayHi()....
Hello zhaoyl