webservice使用CXF的简单实现

webservice使用CXF的简单实现

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

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

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

代码

这是一个模拟的接口:

package webservice;

import javax.jws.WebService;

@WebService     //webservice sign annotation
public interface IWeatherService {
    public String getWeatherByCityName(String cityName) ;

}

实现类,即具体的webservice

package webservice;

import javax.jws.WebService;

@WebService     //webservice sign annotation
public 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();
    }
}

然后结果如下
运行结果

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值