WS之cxf简单实现

1、服务端实现:

  1.1 定义接口,用@WebService修饰:

    /** @WebService 所修饰的接口,那么接口里面的方法全部都属于web的服务  */
    @WebService
    public interface HelloWorld {
       //基本数据类型都可以CXF进行实习
       String sayHello(String name);
    }

  1.2 实现接口:

  public class HelloWorldImpl implements HelloWorld {

     @Override
     public String sayHello(String name) {  
        return name + ",你好,现在的时间是: "+new Date();
     }
  }

  1.3 发布:

    public class HelloWorldServiceTest {
       public static void main(String[] args) {
  
          /** 发布web service */
          Endpoint.publish("http://127.0.0.1:8080/helloService", new HelloWorldImpl());
       }
    }

    发布之后,在浏览器中通过 http://127.0.0.1:8080/helloService?wsdl 访问,如果返回xml信息说明发布成功。

2、客户端实现:

  2.1 建立一个java项目,通过cmd命令进入src目录下,执行命令 wsdl2java http://127.0.0.1:8080/helloService?wsdl ,之后该项目下就会生成很多文件。

    通过  http://127.0.0.1:8080/helloService?wsdl=HelloWorld.wsdl   地址就可以查看该webservice发布的所有方法,以及方法参数,类型和返回值类型情况。

    

   2.2 客户端测试:

    public class HelloWorldServiceTest {

       public static void main(String[] args) {
          HelloWorldImplService factory= new HelloWorldImplService();
          HelloWorld hw = factory.getHelloWorldImplPort();  //获取的是getXXXPort,其中XXX表示实现类名称
          String s = hw.sayHello("tom");
          System.out.println(s);
       }
    }

  3.基于上面实现过程的总结:

  

  4、需要引入的jar文件:

  

原文地址:https://www.cnblogs.com/lbangel/p/3163784.html