CXF-HelloWorld-Java first

来源:互联网 发布:java昵称自动生成器 编辑:程序博客网 时间:2024/06/15 02:21

1.eclipse中新建java工程,导入cxf包

接口HelloWorld

public interface HelloWorld {
 public String sayHi();
}

 

接口实现HelloWorldImpl
public class HelloWorldImpl implements HelloWorld {

 public String sayHi() {
  // TODO Auto-generated method stub
  return "Hi";
 }

}

Server类

import org.apache.cxf.frontend.ServerFactoryBean;

public class Server {
     protected Server() throws Exception {
        HelloWorldImpl 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 {
        new Server();
        System.out.println("Server ready...");

        Thread.sleep(5 * 60 * 1000);
        System.out.println("Server exiting");
        System.exit(0);
    }
}

Client类

import org.apache.cxf.frontend.ClientProxyFactoryBean;

public class Client {

    private Client() {
    }
    public static void main(String args[]) {
        ClientProxyFactoryBean factory = new ClientProxyFactoryBean();
        factory.setServiceClass(HelloWorld.class);
        if (args != null && args.length > 0 && !"".equals(args[0])) {
            factory.setAddress(args[0]);
        } else {
            factory.setAddress("http://localhost:9000/Hello");
        }
        HelloWorld client = (HelloWorld) factory.create();
        System.out.println("Invoke sayHi()....");
        System.out.println(client.sayHi());
        System.exit(0);
    }
}

运行server类后 访问http://localhost:9000/Hello能看到wsdl

运行client类

 

 

 

原创粉丝点击