【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. C#下实现软件欢迎界面

    找到几种简约的欢迎界面的制作方法,存此记录. 方法一:双线程,用第二个线程启动欢迎界面 原文:http://www.cnblogs.com/xiaoshatian/archive/2010/09/07 ...

  2. Oracle- 存储过程和异常捕捉

    这段时间晚上有时候去打打球,回家看看电视剧,日子一天天过…….学了点ORACLE存储过程基础,作一下备注,以便日后需查阅. 创建无参存储过程 create procedure p_myPro1 is ...

  3. ASP.NET 学习的总结

    应用程序域 使用.Net建立的可执行程序*.exe,并没有直接承载到进程当中,而是承载到应用程序域(AppDomain)当中.应用程序域是.Net引入的一个新概念,它比进程所占用的资源要少,可以被看做 ...

  4. cocos2d-x 技能冷却特效

    转自:http://blog.csdn.net/qiurisuixiang/article/details/8779540 1 在CSDN上看到某同学实现的Dota技能冷却效果,自己平时也玩Dota, ...

  5. 分析Model2系统心得

    分析Model2系统心得 前言:观摩他人的项目,学到一些新的.实践经验呀!!! 1.  怎样使用字符串处理类?从页面获取的Form类或者字段取值时使用. 2.在验证用户身份时,先推断username, ...

  6. .@RequestMapping 使用方法

    1.@RequestMapping  使用方法  SpringMVC中,@RequestMapping用来处理请求,比方XXX.do @RequestMapping("/aaa") ...

  7. delphi中使用webbrowser提交表单

    转自:http://blog.163.com/hehaifeng1984@126/blog/static/6900113620133504644998/ 我们以百度搜索为例,使用webbrowser组 ...

  8. leetcode第一刷_Jump Game

    这个题事实上非常easy的,我一開始想复杂了,它没要求记录路径,事实上仅仅要看一下每一步之后所能延伸到的最远的位置就能够了,在这一个最远位置前面的那些位置,都是能够到达的,假设扫到了某个i,它大于当前 ...

  9. 文件和目录之link、unlink、remove和rename函数

    任何一个文件可以有多个目录项指向其i节点.创建一个指向现有文件的链接的方法是使用link函数. #include <unistd.h> int link( const char *exis ...

  10. AsyncTask的用法

    在开发Android应用时必须遵守单线程模型的原则: Android UI操作并不是线程安全的并且这些操作必须在UI线程中执行.在单线程模型中始终要记住两条法则: 1. 不要阻塞UI线程 2. 确保只 ...