webservice使用CXF的简单实现

来源:互联网 发布:服务器centos和ubuntu 编辑:程序博客网 时间:2024/06/13 12:52

webservice使用CXF的简单实现

记录下使用的webservcie
这里是一个简单demo
根据基本知识点,这个是Jax-ws
Web Service本身其实是在实现应用程序间的通信。也可以说是两个项目,两台服务器之间。

Web Service平台需要一套协议来实现分布式应用程序的创建。任何平台都有它的数据表示方法和类型系统。要实现互操作性,Web Service平台必须提供一套标准的类型系统,用于沟通不同平台、编程语言和组件模型中的不同类型系统。这些协议有: —— [ 参见百度百科 ]

其实应该多描述下。。但今天加班有点累。。而且对于简单的demo。。我的语言确实很贫乏,,那就直接贴代码了

代码

这是一个模拟的接口:

package webservice;import javax.jws.WebService;@WebService     //webservice sign annotationpublic interface IWeatherService {    public String getWeatherByCityName(String cityName) ;}

实现类,即具体的webservice

package webservice;import javax.jws.WebService;@WebService     //webservice sign annotationpublic interface IWeatherService {    public String getWeatherByCityName(String cityName) ;}

写个main方法跑一下,懒得导Junit

package publisher;import org.apache.cxf.jaxws.JaxWsServerFactoryBean;import webservice.WeatherService;public class PublisherTest {    public static void main(String[] args) {        JaxWsServerFactoryBean factoryBean =new JaxWsServerFactoryBean();        factoryBean.setAddress("http://localhost:12345/weather");        factoryBean.setServiceBean(new WeatherService());        factoryBean.create();    }}

这里新建一个项目,然后把server用的包拷过来。
生成的代码结构如下:
代码结构
然后测试跑下

package webservice.invoking;import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;public class WeatherServiceInvokingClient {    public static void testWeather(){        //1.生成一个客户端代理工厂        JaxWsProxyFactoryBean client = new JaxWsProxyFactoryBean();        //2.设置服务端的访问地址        client.setAddress("http://localhost:12345/weather?wsdl");        //3.设置服务端的接口        client.setServiceClass(IWeatherService.class);        //4.创建客户端对象        IWeatherService iws = (IWeatherService) client.create();        //5.调用远程的服务端提供的方法        String result = iws.getWeatherByCityName("Shenyang");        System.out.println(result);    }    public static void main(String[] args) {        testWeather();    }}

然后结果如下
运行结果

下次写项目中怎么用,写的不好,有错误请指正。