1. 搭建服务端(查询天气)

// 1. 引入cxf的 jar 包;
// 2. 创建 SEI 接口, 需要加入注解: @WebService
@WebService
public interface WeatherInterface{
public String queryWeather(String cityName);
} // 3. 创建 SEI 实现类
public class WeatherImpl implements WeatherInterface{ System.out.println("from client..."+cityName); if("北京".equals(cityName)){
return "冷且霾";
}else{
return "晴空万里";
}
} // 4. 发布服务, 即服务端
public class WeatherServer{
public static void main(String[] args){
// 创建发布服务类 JaxWsServerFactoryBean
JaxWsServerFactoryBean jwsf = new JaxWsServerFactoryBean(); // 设置服务接口
// 设置服务接口或服务实现类,二者选其一即可;
jwsf.setServiceClass(WeatherInterface.class); // 设置服务实现类
jwsf.setServiceBean(new WeatherImpl()); // 设置服务地址
jwsf.setAddress("http://127.0.0.1:12345/weather"); // 发布服务
jwsf.create();
}
} // 5. 测试服务是否发布成功
// 访问: http://127.0.0.1:12345/weather?wsdl
// 阅读使用说明书,确定客户端调用的接口,方法,参数和返回值 // 发布 SOAP1.2 的服务端
// 需要在接口上加入如下注解: @BindingType(SOAPBinding.SOAP12HTTP_BINDING)
@WebService
@BindingType(SOAPBinding.SOAP12HTTP_BINDING)
public interface WeatherInterface{
public String queryWeather(String cityName);
}

2. 生成客户端(接收天气)

  1. Wsdl2java命令是CXF提供的生成客户端工具,与wsimport类似,可以根据 WSDL 生成客户端代码;
  2. 常用参数:
    • -d: 指定输出目录;
    • -p: 指定包名,如果不指定该参数,默认包名是 WSDL 的命名空间倒序;
  3. Wsdl2java 支持 SOAP1.1 和 SOAP1.2;
// 1. 使用 wsdl2java 命令生成客户端代码
// 注意: 此处需要配置 apache-cxf-3.2.0 中的 bin 目录到 PATH // 2. 使用说明书,使用生成代码调用服务端
public class WeatherClient{
public static void main(String[] args){
// JaxWsProxyFactoryBean 调用服务端
JaxWsProxyFactoryBean jwpf = new JaxWsProxyFactoryBean(); // 设置服务接口
jwpf.setServiceClass(WeatherInterface.class); // 设置服务地址
jwpf.setAddress("http://127.0.0.1:12345/weather"); // 获取服务接口实例
WeatherInterface wif = (WeatherInterface)jwpf.create(); // 调用查询方法
String weather = wif.queryWeather("北京");
System.out.println(weather);
}
}

3. CXF 拦截器

  1. 特点: 拦截可以拦截请求和响应,从而可以获取请求和响应的信息;
  2. 使用:
    • 拦截器必须加入到服务端,并且是在服务端发布之前;
    • 获取拦截器列表,将自定义的拦截器加入到列表中;
//  服务端
public class WeatherServer{
public static void main(String[] args){ .....同上 // 设置服务地址
jwsf.setAddress("http://127.0.0.1:12345/weather"); // 加入拦截器
jwsf.getInInterceptors().add(new LoggingInInterceptor());
jwsf.getOutInterceptors().add(new LoggingOutInterceptor()); // 发布服务
jwsf.create();
}
}

4. CXF+Spring 整合发布SOAP协议的服务

// 1. 创建web 项目,引入 jar 包

// 2. 创建 SEI 接口, 需要加入注解: @WebService
@WebService
@BindingType(SOAPBinding.SOAP12HTTP_BINDING)
public interface WeatherInterface{
public String queryWeather(String cityName);
} // 3. 创建 SEI 实现类
public class WeatherImpl implements WeatherInterface{ System.out.println("from client..."+cityName); if("北京".equals(cityName)){
return "冷且霾";
}else{
return "晴空万里";
}
} // 4. 配置 applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cxf="http://cxf.apache.org/core"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/core
http://cxf.apache.org/schemas/core.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd"> <!-- jaxws:server 发布SOAP 协议的服务, 对 JaxWsServerFactoryBean 类封装 -->
<!-- 配置服务地址和服务接口 -->
<jaxws:server address="/weather" serviceClass="com.itcast.service.WeatherInterface">
<jaxws:serviceBean>
<ref bean="weatherInterface"/>
</jaxws:serviceBean> <!-- 配置拦截器 -->
<jaxws:inInterceptors>
<ref bean="inInterceptor"/>
</jaxws:inInterceptors>
<jaxws:outInterceptors>
<ref bean="outInterceptor"/>
</jaxws:outInterceptors>
</jaxws:server> <!-- 配置服务实现类 -->
<bean name="weatherInterface" class="com.itcast.service.WeatherImpl"/> <!-- 配置拦截器的Bean -->
<bean name="inInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor"/>
<bean name="outInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>
</beans> // 5. 配置 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5"> <!-- 配置Spring 框架整合WEB监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- 加载方式:默认只能加载 WEB-INF 目录下的配置文件; 自定义配置方式,加载 src 目录下配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param> <!-- 配置CXF的 Servlet -->
<servlet>
<servlet-name>CXF</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXF</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
</web-app> // 6. 测试服务,阅读使用说明书
// 访问:http://localhost:8080/项目名/ws/weather?wsdl

4.1 EndPoint 方式发布服务

// 不需要接口,只需要提供类即可
@WebService
public class HelloWorld{
public String sayHello(String name){
return "hello, "+ name;
}
} // applicationContext.xml 配置
<!-- jaxws:endpoint 发布SOAP协议服务, 对Endpoint类封装 -->
<jaxws:endpoint address="/hello" implementor="cn.itcast.demo.HelloWorld"/>

4.2 生成客户端

// applicationContext.xml 配置
<!-- jaxws:client 实现客户端,其实就是对 JaxWsProxyFactoryBean 的封装 -->
<jaxws:client id="weatherClient" address="http://localhost:8080/项目名/ws/weather"
serviceClass="cn.itcast.demo.WeatherInterface"/> // 客户端代码
public class WeatherClient{
public static void main(String[] args){
// 初始化 spring 的上下文
ApplicationContext context =
new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
WeatherInterface wif = (WeatherInterface)context.getBean("weatherClient"); // 调用查询方法
wif.queryWeather("北京");
}
}

5.CXF发布REST服务

5.1 REST概述

  • REST 是一种软件架构模式,可以对资源进行更加准确的定位;
  • 非REST方式:http://localhost:8080/queryUser.action?username=123&id=001;
  • REST方式:http://localhost:8080/user/name/query/001;
  • REST 支持数据格式: XML,JSON;
  • REST 支持发送方式: GET,POST;
// 需求:
// 查询单个学生
// 查询多个学生 // 搭建服务端
/*
* 1. 导入 jar 包;
* 2. 创建学生实体类;
* 3. 创建SEI接口;
* 4. 创建SEI实现类;
* 5. 发布服务;
* 6. 测试服务;
*/ // 2. 创建学生实体类
// @XmlRootElement 可以实现对象和XML数据之间的转换
@XmlRootElement(name="student")
public class Student{
private Long id;
private String name;
private Date birthday; get 和 set 方法;
} // 3. 创建 SEI 接口
// @Path("/student") 就是将请求路径中的"/student"映射到接口
@WebService
@Path("/student")
public interface Studentinteface(){ // 查询单个学生
// @Path("/query/{id}") 就是将"/query"映射到方法上, "{id}"映射到参数上
// 如果有多个参数: @Path("/query/{参数1}/{参数2}/...")
// @GET 指定请求方式; 如果服务端指定的是 GET(POST),那么客户端访问时,必须使用GET(POST)
@GET
@Produces(MediaType.APPLICATION_XML) // 指定服务数据类型
@Path("/query/{id}")
public Student query(@PathParam("id")Long id); // 查询多个学生
@GET
@Produces("application/json;charset=utf-8") // json格式,需要解决乱码问题
@Path("querylist/{name}")
public List<Student> queryList(@PathParam("name")String name);
} // 4. 创建 SEI 实现类
public class StudentImpl implements StudentInterface{ public Student query(Long id){
Student stu = new Student();
stu.setId((long)110);
stu.setName("张三");
stu.setBirthday(new Date()); return stu;
} public List<Student> queryList(String name){
Student stu = new Student();
stu.setId((long)110);
stu.setName("张三");
stu.setBirthday(new Date()); Student stu2 = new Student();
stu2.setId((long)120);
stu.setName("李四");
stu.setBirthday(new Date()); List<Student> list = new ArrayList<Student>(); list.add(stu1);
list.add(stu2);
return list;
}
} // 5. 发布服务
public class StudentServer{
public static void main(String[] args){
// JAXRSServerFactoryBean 发布REST服务
JAXRSServerFatoryBean jrfb = new JAXRSServerFatoryBean(); // 设置服务实现类
jrfb.setServiceBean(new StudentImpl()); // 设置资源类
jrfb.setResourceClasses(StudnetImpl.class); // 设置服务地址
jrfb.setAddress("http://127.0.0.1:12345/user"); // 发布服务
jrfb.create();
}
} // 6. 测试服务
// 查询单个学生, 访问: http://127.0.0.1:12345/user/student/query/110
// 查询多个学生, 访问: http://127.0.0.1:12345/user/student/querylist/lisi // 备注:
// 如果在同一方法上,同时指定XML和JSON媒体类型,在GET请求下,默认返回XML, 在POST请求下,默认返回JSON
// 另外,可以使用如下方式,访问指定格式的数据
// http://127.0.0.1:12345/user/student/querylist/lisi?_type=xml(或?_type=json)

6. CXF+Spring整合发布REST服务

// 服务端
// 1. 导入 jar 包
// 2. 创建 Student 实体类(同上)
// 3. 创建 SEI 接口(同上)
// 4. 创建 SEI 实现类(同上)
// 5. 配置 applicationContext.xml
<!-- jaxrs:server 发布REST服务 -->
<jaxrs:server address="/user">
<jaxrs:serviceBeans>
<ref bean="studentInterface"/>
</jaxrs:serviceBeans>
</jaxrs:server> <!-- 配置服务实现类 -->
<bean name="studentInterface" class="cn.itcast.server.StudentImpl"/> // 6. web.xml 配置(同CXF+Spring发布SOAP服务)
// 7. 测试服务
// REST 服务的说明书地址: http://localhost:8080/项目名/ws/user?_wadl
// 访问: http://localhost:8080/项目名/ws/user/student/query/110

参考资料

CXF 框架的更多相关文章

  1. 【Java EE 学习 81】【CXF框架】【CXF整合Spring】

    一.CXF简介 CXF是Apache公司下的项目,CXF=Celtix+Xfire:它支持soap1.1.soap1.2,而且能够和spring进行快速无缝整合. 另外jax-ws是Sun公司发布的一 ...

  2. iOS交互WebService(cxf框架)

    公司后台java用的cxf框架,说是iOS.Android.web客户端都可以通用,但是我还是第一次遇到,所以做的时候遇到了不小的坑.下面总结下我开发中遇到的问题以及解决方案: 首先,后台提供了一份接 ...

  3. WebService之CXF框架

    本文主要包括以下内容 ant工具的使用 利用cxf实现webservice cxf与spring整合 ajax访问webservice ant 工具 1.为什么要用到ant这个工具呢? Ant做为一种 ...

  4. webservice第三篇【接口开发webservice、CXF框架使用、IDEA下使用webservice、小例子】

    实现接口的webservice 服务端 import javax.jws.WebService; /**面向接口的webservice发布方式 * * */ @WebService public in ...

  5. Web服务cxf框架发布2

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本人声明.否则将追究法律责任. 作者:永恒の_☆ 地址:http://blog.csdn.net/chenghui0317/ ...

  6. webservice的简单使用,cxf框架的的使用

    Web service是一个平台独立的,低耦合的,自包含的.基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述.发布.发现.协调和配置这些应用程序,用于开发分布 ...

  7. 转载 WebService 的CXF框架 WS方式Spring开发

    WebService 的CXF框架 WS方式Spring开发   1.建项目,导包. 1 <project xmlns="http://maven.apache.org/POM/4.0 ...

  8. WebService系列二:使用JDK和CXF框架开发WebService

    一.使用JDK开发WebService 服务端程序创建: 1.新建一个JDK开发webservice的服务端maven项目JDKWebServiceServer 2. 定义一个接口,使用@WebSer ...

  9. Web Service与Apache CXF 框架

    一.WebService简介 为了支持跨网络的机器间相互操作交互而设计,用于开发分布式的互操作的应用程序组件. Web Service服务通常被定义为一组模块化的API,它们可以通过网络进行调用,来执 ...

  10. CXF框架介绍及Spring集成

    1.CXF框架概念介绍 Apache CXF 是一个开源的 WebService 框架,CXF可以用来构建和开发 WebService,这些服务可以支持多种协议,比如:SOAP.POST/HTTP.H ...

随机推荐

  1. 怎样使用Debussy+ModelSim快速查看前仿真波形

    引子:ModelSim是HDL仿真软件,Debussy是波形查看软件:搭配使用,相当爽.此处所谓快速查看前仿真波形仅为抛砖引玉,大家不要拘泥于此.两款软件的功能都很强大,请自行研究. 注:本篇博文的软 ...

  2. php 给图片增加背景平铺水印代码

    如果你想利用php 给图片增加背景平铺水印效果话,必须利用php的一个插件来实例,就是利用imagick,他可以给图片增加背景平铺水印效果哦,下面我们提供一款实例代码. 如果你想利用php教程 给图片 ...

  3. Compiler Error C2872: ambiguous symbol

    参考资料:http://blog.csdn.net/greytree/article/details/354530 刚才写的程序报错ERROR C2872(CL.exe)原因很简单 ZThread有定 ...

  4. 设置VMware随系统开机自动启动并引导虚拟机操作系统

    设置VMware随系统开机自动启动并引导虚拟机操作系统 转载 2012年03月15日 19:50:53 标签: vmware / 虚拟机 / windows / parameters / tools  ...

  5. WebService 页面重定向错误

    “/”应用程序中的服务器错误. 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败. xxx.xxx.xxx.xxx:xx 说明: 执行当前 Web 请求期间,出现未经处理的异常.请 ...

  6. PHP上传类 图片上传 upload class实现image crop resize 缩略图

    manage uploaded files, and manipulate images in many ways through an HTML form, a Flash uploader, XM ...

  7. Respond.js – 让不懂爱的 IE6-8 支持 CSS3 Media Query

    respond.min.js <script src="js/respond.min.js"></script> respond.min.js代码: /*! ...

  8. Easyui data方法扩展finder

    finder: function(jq, conditions){ if(!$(jq).data("OriginalData")){ $(jq).data("Origin ...

  9. CodeSmith自动生成代码使用

    官网地址:http://www.codesmithtools.com/ CodeSmith开发系列资料总结 http://terrylee.cnblogs.com/archive/2005/12/28 ...

  10. Linux下面 多线程死锁问题的调试

    最近写服务,经常是单进程,多线程的,加了各种锁,很担心出现死锁问题,专门学习了一下死锁问题的诊断. 死锁 (deallocks): 是指两个或两个以上的进程(线程)在执行过程中,因争夺资源而造成的一种 ...