1.1cxf入门
从官网下载cxf压缩包
JAVA_HOME,
CXF_HOME=cxf的目录
Path =%JAVA_HOME%\bin;%CXF_HOME%\bin;
CLASSPATH=.;%CXF_HOME%\lib\cxf-manifest.jar
1.2cxf第一个程序:
1.2.1 服务端:
1.2.1.1开发SEI及SEI实现类:
Cxf开发SEI需要将@webservice注解加在接口中.
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);
}
SEI实现类:
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;
}
}
1.2.1.2发布服务代码:
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/weather");
//指定 porttype
jaxWsServerFactoryBean.setServiceClass(WeatherInterface.class);
//指定服务类对象
jaxWsServerFactoryBean.setServiceBean(new WeatherInterfaceImpl());
//发布服务
jaxWsServerFactoryBean.create();
}
}
1.3.3小结:
Cxf编程方式实现webservice:
JaxWsServerFactoryBean:发布webservice服务
JaxWsProxyFactoryBean:调用webservice服务端
Cxf和spring整合后,可以将上边的编程方式修改为spring配置方式实现。
内部使用JaxWsServerFactoryBean和JaxWsProxyFactoryBean
1.4cxf发布soap1.2:
使用cxf发布soap1.2不需要下载jaxws-ri扩展包。
在服务接口的上面添加
@BindingType(javax.xml.ws.soap.SOAPBinding.SOAP12HTTP_BINDING)
如下:
@WebService
@BindingType(javax.xml.ws.soap.SOAPBinding.SOAP12HTTP_BINDING)
publicinterface WeatherServerInterface