12_CXF入门

时间:2022-02-18 07:10:38

【CXF】

  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方式。

  灵活的部署:可以运行有Tomcat、Jboss、Jetty(内置)、WebLogic上面。

【环境配置】

下载apache-cxf-2.7.18版本

12_CXF入门

环境变量配置:

CXF_HOME= D:\Program Files\apache-cxf-2.7.18
Path = %CXF_HOME%\bin;
CLASSPATH=%CXF_HOME%\lib\cxf-manifest.jar

【创建CXF工程流程】

第一步:创建java工程

第二步:将CXF的jar包加入工程

第三步:创建服务接口和服务实现类(创建服务接口和服务类的方法同上篇的描述,编写SEI及SEI的实现)

【第一个CXF程序】

【服务端工程截图】

12_CXF入门

【WeatherModel.java】

package com.Higgin.ws.pojo;

import java.util.Date;

public class WeatherModel {

    //天气概况
private String detail; //日期
private Date date; //最高温度
private int temperature_max; //最低温度
private int temperature_min;
//忽略get/set方法
}

【WeatherInterface.java】

注意:CXF开发SEI需要将@WebService注解放在接口中(之前是在接口实现类中)

package com.Higgin.ws.service;

import java.util.List;

import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.ws.BindingType; import com.Higgin.ws.pojo.WeatherModel; @WebService(
targetNamespace
="http://weather.Higgin.com/",//指定wsdl的命名空间
name="WeatherInterface", //指定portType的名称
portName="WeatherInterfacePort", //指定port的名称
serviceName="WeatherService" //服务视图的名称
//endpointInterface="com.Higgin.ws.service.WeatherInterface" //指定哪个接口中方法要发布成WebService,此时接口中也要加上@WebService注解
)
public interface WeatherInterface {
public @WebResult(name="result") List<WeatherModel> queryWeather(@WebParam(name="cityName") String cityName);
}

【WeatherInterfaceImpl.java】

package com.Higgin.ws.service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import com.Higgin.ws.pojo.WeatherModel; //这里没有@WebService注解!!!!!
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.setDate(new Date());
weatherModel_1.setTemperature_max(10);
weatherModel_1.setTemperature_min(-10); WeatherModel weatherModel_2=new WeatherModel();
weatherModel_2.setDetail("阴天");
calendar.set(Calendar.DATE, day+1);
weatherModel_2.setDate(calendar.getTime());
weatherModel_2.setTemperature_max(6);
weatherModel_2.setTemperature_min(-2); WeatherModel weatherModel_3=new WeatherModel();
weatherModel_3.setDetail("晴天");
calendar.set(Calendar.DATE, day+2);
weatherModel_3.setDate(calendar.getTime());
weatherModel_3.setTemperature_max(30);
weatherModel_3.setTemperature_min(3); list.add(weatherModel_1);
list.add(weatherModel_2);
list.add(weatherModel_3);
return list;
} }

【WeatherServer.java】发布服务代码

package com.Higgin.ws.service;

import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

public class WeatherServer {
public static void main(String[] args) {
//Endpoint.publish("http://127.0.0.1:12345/weather", new WeatherInterfaceImpl()); //使用jaxWs发布SOAP协议的WebService
JaxWsServerFactoryBean jaxWsServerFactoryBean=new JaxWsServerFactoryBean();
//指定WebService地址
jaxWsServerFactoryBean.setAddress("http://127.0.0.1:12345/weather");
//指定portType
jaxWsServerFactoryBean.setServiceClass(WeatherInterface.class);
//指定服务类对象
jaxWsServerFactoryBean.setServiceBean(new WeatherInterfaceImpl());
//发布服务
jaxWsServerFactoryBean.create();
}
}

【运行服务代码之后,使用Wsimport生成客户端代码,并导入客户端】

12_CXF入门

12_CXF入门

【客户端工程截图】

12_CXF入门

【WeatherClient.java】

package com.higgin.weather.client;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; import com.higgin.weather.WeatherInterface;
import com.higgin.weather.WeatherModel; public class WeatherClient {
public static void main(String[] args) {
//jaxWsProxyFactoryBean调用WebService服务端
JaxWsProxyFactoryBean jaxWsProxyFactoryBean=new JaxWsProxyFactoryBean();
//调用地址
jaxWsProxyFactoryBean.setAddress("http://127.0.0.1:12345/weather?wsdl");
//设置portType
jaxWsProxyFactoryBean.setServiceClass(WeatherInterface.class);
//创建portType
WeatherInterface weatherInterface=(WeatherInterface) jaxWsProxyFactoryBean.create(); //调用portType方法
List<WeatherModel> list=weatherInterface.queryWeather("杭州"); for(WeatherModel weatherModel:list){
System.out.println(weatherModel.getDetail());
Date date=weatherModel.getDate().toGregorianCalendar().getTime();
System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(date));
System.out.println(weatherModel.getTemperatureMax());
System.out.println(weatherModel.getTemperatureMin());
} }
}

【运行结果】

12_CXF入门