• 1.倒入cxf所需要的包(其中包括spring的支持)
  • 2.编写SEI与服务
package cn.itcast.ws.pojo;
import java.util.Date;
public class WeatherModel {//天气概况private String detail;//日期private Date data;//最高温度private int temperature_max;//最低温度private int temperature_min;public String getDetail() {return detail;}public void setDetail(String detail) {this.detail = detail;}public Date getData() {return data;}public void setData(Date data) {this.data = data;}public int getTemperature_max() {return temperature_max;}public void setTemperature_max(int temperature_max) {this.temperature_max = temperature_max;}public int getTemperature_min() {return temperature_min;}public void setTemperature_min(int temperature_min) {this.temperature_min = temperature_min;}
}
@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);}
package cn.itcast.ws.service;import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;import cn.itcast.ws.pojo.WeatherModel;public class WeatherInterfaceImpl implements WeatherInterface {@Overridepublic  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;}
}
  • 3.开始搞定服务端的配置问题

    • 在sourse folder的config下添加applicationContext
<?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:jaxws="http://cxf.apache.org/jaxws"xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsdhttp://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsdhttp://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd"><!-- service -->
<bean id="weatherInterface" class="cn.itcast.ws.service.WeatherInterfaceImpl"/><!-- 发布服务
使用jaxws:server和jaxws:endpoint可以发布服务
webservice地址=tomcat地址+cxf servlet的路径+/weather-->
<jaxws:server address="/weatherss" serviceClass="cn.itcast.ws.service.WeatherInterface"><jaxws:serviceBean><ref bean="weatherInterface"/></jaxws:serviceBean>
</jaxws:server>
</beans>
  • 4.运行tomcat,部署项目

  • 5.客户端开始创建

先用cxf在吗自动生成工具

wsdl2java -d XXX http://127.0.0.1:12345/weath?wsdl

XXX表示代码生成的本地目录

  • 6 同样在客户端上也添加cxf的jar包,添加applicationComtext.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:jaxws="http://cxf.apache.org/jaxws"xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsdhttp://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsdhttp://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd"><!-- 使用<jaxws:client调用服务端
jaxws:client内部使用JaxWsProxyFactoryBean方式
serviceClass:指定portType地址(需要使用wsdl2java工具生成)-->
<jaxws:client id="weatherClient" address="http://localhost:8080/ws_1231_cxf_spring_server/ws/weatherss?wsdl"serviceClass="cn.itcast.weather.WeatherInterface"></jaxws:client></beans>
  • 7.调用服务方法
package cn.itcast.ws.cxf;import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import cn.itcast.weather.WeatherInterface;
import cn.itcast.weather.WeatherModel;/*** * <p>* Title: ClientTest* </p>* <p>* Description: cxf+spring整合,客户端测试* </p>* <p>* Company: www.itcast.com* </p>
public class ClientTest {private ApplicationContext applicationContext;@Beforepublic void before() {applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");}@Testpublic void testCxfSpringClient() {// 从spring容器中取出porttypeWeatherInterface weatherInterface = (WeatherInterface) applicationContext.getBean("weatherClient");// 调用portType的方法List<WeatherModel> list = weatherInterface.queryWeather("郑州");for (WeatherModel weatherModel : list) {System.out.println(weatherModel.getDetail());Date date = weatherModel.getData().toGregorianCalendar().getTime();System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(date));System.out.println(weatherModel.getTemperatureMax());System.out.println(weatherModel.getTemperatureMin());}}}

代码地址
https://github.com/ruiruiruiruirui22/cxf_spring

spring与cxf的整合相关推荐

  1. Spring和CXF整合发布WebService(服务端、客户端)

    参考Spring和CXF整合发布WebService(服务端.客户端) 转载于:https://www.cnblogs.com/timspace/p/11113576.html

  2. CXF WebService整合Spring

    CXF WebService整合Spring 首先,CXF和spring整合需要准备如下jar包文件: 这边我是用Spring的jar包是Spring官方提供的,并没有使用CXF中的Spring的ja ...

  3. CXF WebService整合SpringMVC的maven项目

    首先推荐博客:http://www.cnblogs.com/xdp-gacl/p/4259481.html   http://blog.csdn.net/hu_shengyang/article/de ...

  4. Spring Boot 2.x整合Quartz

    宣传官网 xb.exrick.cn 在线Demo xboot.exrick.cn 开源版Github地址 github.com/Exrick/x-bo- 开发文档 www.kancloud.cn/ex ...

  5. velocity mybatis spring 在maven的整合开发(二)

    2019独角兽企业重金招聘Python工程师标准>>> 对于一个web项目,位于WEB-INFO下的web.xml永远是该项目的总配置. 我的web.xml <?xml ver ...

  6. spring和CXF集成来实现webservices

    最近在负责一个大系统的实施,经过需求分析之后,将系统分为5个子系统,我们采用SOA架构,分模块开发.项目组中最大的一个争议就是,子系统之间的通讯问题,大家提出了两种方案:一.如果5个子系统最后发布为5 ...

  7. Struts2与Spring、Hibernate三者整合的过程示例

    转载地址:http://www.360doc.com/content/09/0416/09/61497_3148602.shtml# 原来spring配置文件自动生成数据源和整合先后有关系,留着做个提 ...

  8. Spring与Quartz的整合实现定时任务调度

    Spring与Quartz的整合实现定时任务调度 摘自: http://kevin19900306.iteye.com/blog/1397744 最近在研究Spring中的定时任务功能,最好的办法当然 ...

  9. Spring与日志的整合

    Spring 与日志框架进行整合,日志框架就可以在控制台中,输出Spring框架运行过程中的⼀ 些重要的信息. 好处:便于了解Spring框架的运行过程,利于程序的调试. 默认日志框架 Spring ...

最新文章

  1. 值得分享!最新发现了10个冷门好用软件,一眼就会爱上
  2. VC中DDX/DDV自定义
  3. 大文件做分割处理的方法——winRAR压缩分割法
  4. 2.3.11 管程
  5. c# 类的基本知识,未完,待续
  6. 二维有限元方程matlab,有限元法求解二维Poisson方程的MATLAB实现
  7. 母版中menu控件上传后出现脚本错误
  8. parseFloat(string)
  9. Unix原理与应用学习笔记----第四章 文件系统2
  10. jsp向servlet传输数据
  11. plsa java代码_LDA主题聚类学习小结
  12. JSON Web Token从入门到精通
  13. 简单循迹小车实验心得_智能小车实验报告
  14. 强烈推荐:创业起步 八种赢利模式
  15. 数据结构与算法分析:算法分析
  16. google scholar 使用不了的问题——已解决
  17. 亚马逊aws 服务器删除_如何关闭Amazon AWS上服务器
  18. 云南计算机专修学校附中,云南昆明这四所重点中学,师资力量雄厚,教学经验丰富!...
  19. 【现代机器人学】学习笔记七:开链动力学(前向动力学Forward dynamics 与逆动力学Inverse dynamics)
  20. Android Jetpack导航组件——Navigation的使用

热门文章

  1. Android 图形驱动初始化
  2. Flink 在又拍云日志批处理中的实践
  3. 在linux中解压.tgz
  4. 云计算学习路线和经典资料推荐
  5. 带你深入理解分布式事务,掌握后台分布式核心技术,PS:送5本!
  6. 干货 | Elasticsearch开发人员最佳实战指南
  7. 如何ALL IN一场技术大会?
  8. 2019 WAIC | 腾讯张正友:人工智能的热与酷
  9. 腾讯安全平台部专家研究员胡育辉:千亿黑产背后的破局之道
  10. 数据存储介质销毁:护航数据安全的最后一公里