package com.cxf.service;
public interface HelloWorldCxfService {
String sayHello(String username);
}
4.创建该接口的实现类
package com.cxf.service;
public class HelloWorldCxfServiceImpl implements HelloWorldCxfService {
public String sayHello(String username) {
return "Hello,"+username;
}
}
5.发布webservice
package com.cxf.server;
import org.apache.cxf.frontend.ServerFactoryBean;
import com.cxf.service.HelloWorldCxfService;
import com.cxf.service.HelloWorldCxfServiceImpl;
public class Server {
public static void main(String[] args) {
HelloWorldCxfServiceImpl cxfServiceImpl= new HelloWorldCxfServiceImpl();
ServerFactoryBean factoryBean=new ServerFactoryBean();
factoryBean.setAddress("http://localhost:8080/hello");
factoryBean.setServiceClass(HelloWorldCxfService.class);
factoryBean.setServiceBean(cxfServiceImpl);
factoryBean.create();
}
}
运行Server,注意不要关闭,在控制台会打印如下信息:
2011-5-11 14:25:30 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://service.cxf.com/}HelloWorldCxfService from class com.cxf.service.HelloWorldCxfService
2011-5-11 14:25:30 org.apache.cxf.endpoint.ServerImpl initDestination
信息: Setting the server's publish address to be http://localhost:8080/hello
2011-05-11 14:25:30.750:INFO::jetty-7.3.1.v20110307
2011-05-11 14:25:30.859:INFO::Started SelectChannelConnector@localhost:8080
2011-05-11 14:25:30.875:INFO::started o.e.j.s.h.ContextHandler{,null}
6.客户端调用
package com.cxf.server;
import org.apache.cxf.frontend.ClientProxyFactoryBean;
import com.cxf.service.HelloWorldCxfService;
public class Client {
public static void main(String[] args) {
ClientProxyFactoryBean factoryBean=new ClientProxyFactoryBean();
factoryBean.setAddress("http://localhost:8080/hello");
factoryBean.setServiceClass(HelloWorldCxfService.class);
HelloWorldCxfService worldCxfService=(HelloWorldCxfService) factoryBean.create();
System.out.println(worldCxfService.sayHello("张三"));
}
}
运行Client代码,控制台打印如下信息:
2011-5-11 14:25:35 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://service.cxf.com/}HelloWorldCxfService from class com.cxf.service.HelloWorldCxfService
Hello,张三