服务接口:
- package server;
- public interface HelloWorld {
- String sayHi(String name);
- }
服务接口实现类:
- package server;
- public class HelloWorldImpl implements HelloWorld {
- @Override
- public String sayHi(String name) {
- System.out.println("server: " + name);
- return name+"--client";
- }
- }
服务发布:
- package server;
- import org.apache.cxf.frontend.ServerFactoryBean;
- public class Server {
- private Server(){
- HelloWorld helloWorldImpl = new HelloWorldImpl();
- ServerFactoryBean svrFactory = new ServerFactoryBean();
- svrFactory.setServiceClass(HelloWorld.class);
- svrFactory.setAddress("http://localhost:9000/Hello");
- svrFactory.setServiceBean(helloWorldImpl);
- svrFactory.create();
- }
- public static void main(String[] args) throws Exception {
- @SuppressWarnings("unused")
- Server s =new Server();
- Thread.sleep(5*60*1000);
- System.exit(0);
- }
- }
客户端调用:
- package client;
- import org.apache.cxf.frontend.ClientProxyFactoryBean;
- import server.HelloWorld;
- public class HelloWorldClient {
- public static void main(String[] args) {
- ClientProxyFactoryBean factory = new ClientProxyFactoryBean();
- factory.setServiceClass(HelloWorld.class);
- factory.setAddress("http://localhost:9000/Hello");
- HelloWorld client = (HelloWorld) factory.create();
- String rsp = client.sayHi("fhd");
- System.out.println("return: " + rsp);
- }
- }