欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

cxf环境搭建与第一个项目

程序员文章站 2022-07-03 17:26:25
...

1.什么是cxf

Cxf,apache下的webservice的开源框架。
Apache CXF = Celtix + Xfire,开始叫 Apache CeltiXfire,后来更名为 Apache CXF 了,以下简称为 CXF。Apache CXF 是一个开源的 web Services 框架,CXF 帮助您构建和开发 web Services ,它支持多种协议,比如:SOAP1.1,1,2、XML/HTTP、RESTful HTTP 或者 CORBA。
Cxf是基于SOA总线结构,依靠spring完成模块的集成,实现SOA方式。

2.环境配置

下载地址:
http://cxf.apache.org/download.html
环境变量 :
CXF_HOME=cxf的目录
PATH = PATH;CXF_HOME\bin;

3.第一个程序

  • 在客户端和服务器端都要导入cxf的lib包

- 第一步写sei及其实现

@WebService(
        targetNamespace="http://weather.itcast.cn/",//指定 wsdl的命名空间
        name="WeatherInterface",//指定portType的名称
        portName="WeatherInterfacePort",//指定port的名称
        serviceName="WeatherService"//服务视图的名称
        //endpointInterface="cn.itcast.ws.service.WeatherInterface2"//指定哪个接口中方法要发布成webservice服务,接口中加上@webservice注解
        )
@BindingType(javax.xml.ws.soap.SOAPBinding.SOAP12HTTP_BINDING)
public interface WeatherInterface {

    //查询三天天气
    public @WebResult(name="result") List<WeatherModel> queryWeather(@WebParam(name="cityName") String cityName);

}
public class WeatherInterfaceImpl implements WeatherInterface {

    @Override
    public  List<WeatherModel> queryWeather(String cityName) {

        //构造三天天气
        List<WeatherModel> list = new ArrayList<WeatherModel>();

        Calendar calendar = Calendar.getInstance();
        int day = calendar.get(Calendar.DATE);

        WeatherModel weatherModel_1  =new WeatherModel();
        weatherModel_1.setDetail("晴");
        weatherModel_1.setData(new Date());
        weatherModel_1.setTemperature_max(5);
        weatherModel_1.setTemperature_min(-6);

        WeatherModel weatherModel_2  =new WeatherModel();
        weatherModel_2.setDetail("阴");
        calendar.set(Calendar.DATE, day+1);
        weatherModel_2.setData(calendar.getTime());
        weatherModel_2.setTemperature_max(10);
        weatherModel_2.setTemperature_min(-3);

        WeatherModel weatherModel_3  =new WeatherModel();
        weatherModel_3.setDetail("晴");
        calendar.set(Calendar.DATE, day+2);
        weatherModel_3.setData(calendar.getTime());
        weatherModel_3.setTemperature_max(2);
        weatherModel_3.setTemperature_min(-9);


        list.add(weatherModel_1);
        list.add(weatherModel_2);
        list.add(weatherModel_3);

        return list;
    }



}

- 第二步:推送服务

public class WeatherServer {

    public static void main(String[] args) {

        //使用jaxWs发布soap协议的webservice
        JaxWsServerFactoryBean jaxWsServerFactoryBean = new JaxWsServerFactoryBean();
        //指定 webservice地址
        jaxWsServerFactoryBean.setAddress("http://127.0.0.1:12345/weath");
        //指定 porttype
        jaxWsServerFactoryBean.setServiceClass(WeatherInterface.class);

        //指定服务类对象
        jaxWsServerFactoryBean.setServiceBean(new WeatherInterfaceImpl());

        //发布服务
        jaxWsServerFactoryBean.create();

    }

}

然后在浏览器中输入
http://127.0.0.1:12345/weath?wsdl就能看到说明文档

第三步 利用cxf工具自动生成调用服务的代码

wsdl2java -d xxx http://127.0.0.1:12345/weath?wsdl
  • 第四步 编写客户端调用服务
package cn.itcast.weather;

import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.List;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

public class WeathClient3 {
    public static void main(String[] args) {
        JaxWsProxyFactoryBean jaxWsProxyFactoryBean=new JaxWsProxyFactoryBean();
        jaxWsProxyFactoryBean.setAddress("http://127.0.0.1:12345/weath?wsdl");
        jaxWsProxyFactoryBean.setServiceClass(WeatherInterface.class);
        WeatherInterface weatherInterface = (WeatherInterface) jaxWsProxyFactoryBean
                .create();

        // 调用portType方法

        List<WeatherModel> list = weatherInterface.queryWeather("郑州");

        for (WeatherModel weatherModel : list) {
            System.out.println(weatherModel.getDetail());
            Date date = weatherModel.getData().toGregorianCalendar().getTime();
            System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(date));
            System.out.println(weatherModel.getTemperatureMax());
            System.out.println(weatherModel.getTemperatureMin());
        }
    }
}

结果

晴
2017-05-27
5
-6
阴
2017-05-28
10
-3
晴
2017-05-29
2
-9