【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版本

环境变量配置:

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程序】

【服务端工程截图】

【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生成客户端代码,并导入客户端】

【客户端工程截图】

【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入门的更多相关文章

  1. Angular2入门系列教程7-HTTP(一)-使用Angular2自带的http进行网络请求

    上一篇:Angular2入门系列教程6-路由(二)-使用多层级路由并在在路由中传递复杂参数 感觉这篇不是很好写,因为涉及到网络请求,如果采用真实的网络请求,这个例子大家拿到手估计还要自己写一个web ...

  2. ABP入门系列(1)——学习Abp框架之实操演练

    作为.Net工地搬砖长工一名,一直致力于挖坑(Bug)填坑(Debug),但技术却不见长进.也曾热情于新技术的学习,憧憬过成为技术大拿.从前端到后端,从bootstrap到javascript,从py ...

  3. Oracle分析函数入门

    一.Oracle分析函数入门 分析函数是什么?分析函数是Oracle专门用于解决复杂报表统计需求的功能强大的函数,它可以在数据中进行分组然后计算基于组的某种统计值,并且每一组的每一行都可以返回一个统计 ...

  4. Angular2入门系列教程6-路由(二)-使用多层级路由并在在路由中传递复杂参数

    上一篇:Angular2入门系列教程5-路由(一)-使用简单的路由并在在路由中传递参数 之前介绍了简单的路由以及传参,这篇文章我们将要学习复杂一些的路由以及传递其他附加参数.一个好的路由系统可以使我们 ...

  5. Angular2入门系列教程5-路由(一)-使用简单的路由并在在路由中传递参数

    上一篇:Angular2入门系列教程-服务 上一篇文章我们将Angular2的数据服务分离出来,学习了Angular2的依赖注入,这篇文章我们将要学习Angualr2的路由 为了编写样式方便,我们这篇 ...

  6. Angular2入门系列教程4-服务

    上一篇文章 Angular2入门系列教程-多个组件,主从关系 在编程中,我们通常会将数据提供单独分离出来,以免在编写程序的过程中反复复制粘贴数据请求的代码 Angular2中提供了依赖注入的概念,使得 ...

  7. wepack+sass+vue 入门教程(三)

    十一.安装sass文件转换为css需要的相关依赖包 npm install --save-dev sass-loader style-loader css-loader loader的作用是辅助web ...

  8. wepack+sass+vue 入门教程(二)

    六.新建webpack配置文件 webpack.config.js 文件整体框架内容如下,后续会详细说明每个配置项的配置 webpack.config.js直接放在项目demo目录下 module.e ...

  9. wepack+sass+vue 入门教程(一)

    一.安装node.js node.js是基础,必须先安装.而且最新版的node.js,已经集成了npm. 下载地址 node安装,一路按默认即可. 二.全局安装webpack npm install ...

随机推荐

  1. Jsp中的pageContext对象

    这个对象代表页面上下文.组要用于访问页面共享数据.使用pageContext可以直接访问request,session,application范围的属性,看看这些jsp的页面: JSP 页面使用 pa ...

  2. CentOS6.2下安装eclipse

    在eclipse官网下载eclipse的linux版本(此处省略下载过程),我下载的是eclipse-jee-indigo-SR2-linux-gtk.tar.gz,下面是安装过程:  1.sudo ...

  3. cocos2d-x中,简单html富文本显示

    作者:HU 转载请注明,原文链接:http://www.cnblogs.com/xioapingguo/p/4037414.html  虽然自从cocos2d-x更新到3.0后,使用freetype, ...

  4. ubuntu安装mysql后不能远程访问的方法

    ubuntu安装mysql后不能远程访问的方法1.mysql>GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%' IDENTIFIED BY 'mypassw ...

  5. 基于注解的Spring MVC整合Hibernate(所需jar包,spring和Hibernate整合配置,springMVC配置,重定向,批量删除)

    1.导入jar watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdG90b3R1enVvcXVhbg==/font/5a6L5L2T/fontsize/400 ...

  6. Android手机中获取手机号码和运营商信息

    代码如下: package com.pei.activity; import android.app.Activity; import android.os.Bundle; import androi ...

  7. 学习笔记之Linux Shell脚本教程:30分钟玩转Shell脚本编程

    Linux Shell脚本教程:30分钟玩转Shell脚本编程 http://c.biancheng.net/cpp/shell/

  8. windows7怎么共享文件夹

    http://jingyan.baidu.com/article/d45ad148f06fef69552b80e6.html

  9. 可扩展的listview--Expandablelistview

    layout.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" x ...

  10. lambda expand in list

    [(lambda x: x*x)(x) for x in range(10)] Or better yet: [x*x for x in range(10)]