使用CXF发布webService

来源:互联网 发布:菜鸟阎王网络剧百度云 编辑:程序博客网 时间:2024/05/16 11:27
1、下载CXF,并配置环境变量
        将bin目录配置到path中
2、新建java project,导入cxf的jar包,最终项目结构如下:
      
3、建立接口及实现类
        1)接口
                   package com.tgb.web.webservice;
import javax.jws.WebService;
@WebService
public interface HelloWorld {
    public String sayHi(String str);
}
        2)实现类:
                   package com.tgb.web.webservice.impl;
import java.util.Date;
import javax.jws.WebService;
import com.tgb.web.webservice.HelloWorld;
@WebService(endpointInterface="com.tgb.web.webservice.HelloWorld",serviceName="HelloWorld")
public class HelloWorldBean implements HelloWorld {
    @Override
    public String sayHi(String str) {
        return "hello,"+str+",现在时间是:"+new Date();
    }
}
4、利用Endpoint发布webService,即客户端如下:
          package lee;
import javax.xml.ws.Endpoint;
import com.tgb.web.webservice.HelloWorld;
import com.tgb.web.webservice.impl.HelloWorldBean;
public class ServerMain {
    public static void main(String[] args) {
        HelloWorld hw=new HelloWorldBean();
        //调用Endpoint的publish方法发布Web Service
        Endpoint.publish("http://192.168.1.108:8088/HelloWorld", hw);
        
        System.out.println("webService 发布成功!");
    }
}
5、在浏览器中访问地址:http://192.168.1.108:8088/HelloWorld?wsdl ,就可以看到发布的wsdl页面了。

=================================================================
客户端:
1、新建立java项目WS_Client,运行cmd,将目录定位到WS_Client项目下的src目录下,然后输入wsdl2java http://192.168.1.108:8088/HelloWorld?wsdl,就可以在客户端生成调用wsdl的代理对象来,如下图
       
       
2、建立测试类ClientMain :
package lee;
import com.tgb.web.webservice.impl.HelloWorld;
public class ClientMain {
    public static void main(String[] args) {
        HelloWorld factory=new HelloWorld();
        com.tgb.web.webservice.HelloWorld hWorld= factory.getHelloWorldWsPort();
        
        System.out.println(hWorld.sayHi("张三"));
    }
}
3、运行测试类,就可以成功调用。最后输出结果如下:
    hello,张三,现在时间是:Wed Mar 23 22:25:24 CST 2016  

1 0
原创粉丝点击