结合spring框架来实现CXF发布SOAP协议的服务,步骤基本相同,所不同的是的多了一些配置项,步骤如下

1. 服务端

第一步:创建web项目(引入jar包)

第二步:创建SEI接口

import javax.jws.WebService;
import javax.xml.ws.BindingType;
import javax.xml.ws.soap.SOAPBinding;@WebService
@BindingType(SOAPBinding.SOAP12HTTP_BINDING)
public interface WeatherInterface {public String QueryWeather(String cityName);
}

第三步:创建SEI实现类

public class WeatherInterfaceImpl implements WeatherInterface {@Overridepublic String QueryWeather(String cityName) {System.out.println("from client..." + cityName);if ("北京".equals(cityName)) {return "晴转多云";} else {return "雨转小雪";}}
}

第四步:配置spring配置文件,applicationContext.xml,用<jaxws:server>标签发布服务,设置1.服务地址;2.设置服务接口;3.设置服务实现类

<?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:server发布SOAP协议的服务 ,对JaxWsServerFactoryBean类封装--><jaxws:server address="/weather" serviceClass="com.zang.ws.cxf.server.WeatherInterface"><jaxws:serviceBean><ref bean="weatherInterface"/></jaxws:serviceBean><!-- 配置拦截器 --><jaxws:inInterceptors><ref bean="inIntercepter"/></jaxws:inInterceptors><jaxws:outInterceptors><ref bean="outIntercepter"/></jaxws:outInterceptors></jaxws:server><!-- 配置拦截器的bean --><bean name="inIntercepter" class="org.apache.cxf.interceptor.LoggingInInterceptor"/><bean name="outIntercepter" class="org.apache.cxf.interceptor.LoggingOutInterceptor"/><!-- 配置服务实现类 --><bean name="weatherInterface" class="com.zang.ws.cxf.server.WeatherInterfaceImpl"/>
</beans>                  

第五步:配置web.xml,配置spring配置文件地址和加载的listener,配置CXF的servlet。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://xmlns.jcp.org/xml/ns/javaee"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"id="WebApp_ID" version="3.1"><display-name>ws_cxf_spring_server</display-name><!-- 设置spring的环境 --><context-param><!--contextConfigLocation是不能修改的 --><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 配置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><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list>
</web-app>

第六步:部署到tomcat下,启动tomcat

第七步:测试服务,阅读使用说明书  地址: http://localhost:8089/ws_cxf_spring_server/ws/weather?wsdl

如果直接创建实现类,可以使用Endpoint标签发布服务。步骤如下

创建实现类

@WebService
public class HelloWorld {public String sayHello(String name){return "hello,"+name;}
}

之前通过创建SEI接口实现时,applicationContext.xml中是用<jaxws:server>标签来发布服务;而直接通过创建类来实现时,applicationContext.xml中应使用<jaxws:endpoint>标签来发布服务。

<!-- <jaxws:endpoint发布SOAP协议的服务 ,对Endpoint类封装-->
<jaxws:endpoint address="/hello" implementor="com.zang.ws.cxf.server.HelloWorld"/>    

重启tomcat,访问说明书  http://localhost:8089/ws_cxf_spring_server/ws/hello?wsdl

项目结构

2. 客户端

第一步:引入jar包

第二步:生成客户端代码  wsdl2java命令,详见客户端实现

第三步:配置spring配置文件,applicationContent.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实现客户端 ,对JaxWsProxyFactoryBean类封装 --><jaxws:client id="weatherClient"address="http://127.0.0.1:8089/ws_cxf_spring_server/ws/weather"serviceClass="com.zang.cxf.weather.WeatherInterface" />
</beans>                  

第四步:从spring上下文件获取服务实现类,调用查询方法,打印

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import com.zang.cxf.weather.WeatherInterface;public class WeatheClient {public static void main(String[] args) {// 初始化spring的上下文ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");// 调用查询方法
        WeatherInterface weatherInterface = (WeatherInterface) context.getBean("weatherClient");String weather = weatherInterface.queryWeather("济南");System.out.println(weather);}
}

项目结构

转载于:https://www.cnblogs.com/zjfjava/p/9022499.html

Web Service——CXF+Spring 整合相关推荐

  1. android调用web service(cxf)实例

    Google为ndroid平台开发Web Service提供了支持,提供了Ksoap2-android相关架包 1.下载该夹包可以直接登录http://code.google.com/p/ksoap2 ...

  2. Web Service详细解析及使用方法

    Web Service详细解析及使用方法 XFire篇.... 2 XFire简介... 2 XFire特性... 2 XFire使用... 3 提供服务实现类... 3 服务类的接口类... 3 配 ...

  3. Spring Web Service 学习之Hello World篇

    http://fuxueliang.iteye.com/blog/175184 Spring Web Service是Spring社区基于Spring提供的一个关注于创建"文档驱动" ...

  4. spring与cxf的整合

    1.倒入cxf所需要的包(其中包括spring的支持) 2.编写SEI与服务 package cn.itcast.ws.pojo; import java.util.Date; public clas ...

  5. 在 MyEclipse 5.1GA 上使用 XFire 编写 Web Service

       开发环境     Sun Java 5+ Eclipse 3.2 +MyEclipse 5.0.0. 概述 本文介绍了使用MyEclipse Web Service来迅速开发和测试一个Hello ...

  6. MyEclipse下开发Web Service(转)

    http://hi.baidu.com/banseon/blog/item/6cf5e2133e0535005baf53d2.html 本文介绍了使用MyEclipse Web Service来迅速开 ...

  7. Web Service技术的SOAP实现

    写在前面的话:本文章由刘源师兄分享,非我原创.此处仅作学习记录之用. 0 SOA 1 简介 1.1 提出 1.2 定义 1.3 特点 1.4 主要实现方式 1.5 架构 1.6 组成元素 1.6.1 ...

  8. Web Service (二) CXF自动发布Web Service(No Spring)

    Web Service实现目前流行的框架主要有两种,cxf和axis这两个框架,下面是这两个框架的优缺点,我们这个项目中使用的是cxf这个框架,首先看一下没有集成spring的时候是怎么实现远程调用的 ...

  9. Apache CXF实现Web Service(3)——Tomcat容器和不借助Spring的普通Servlet实现JAX-RS(RESTful) web service...

    起步 参照这一系列的另外一篇文章: Apache CXF实现Web Service(2)--不借助重量级Web容器和Spring实现一个纯的JAX-RS(RESTful) web service 首先 ...

最新文章

  1. java旅游系统项目经验_谁能跟我介绍一下Java 项目经验,刚进入这个行业。
  2. ubuntu 将某个目录下的文件复制到_Linux下处理隐私骚操作
  3. Volley框架学习
  4. 基于vue-cli配置移动端自适应
  5. python dlib学习(十一):眨眼检测
  6. 使用Mockito测试Spring组件
  7. bootstrap 2021-04-20
  8. Teamcenter 入门开发系列问答(1)
  9. IBM Machine Learning学习笔记(一)——Exploratory Data Analysis for Machine Learning
  10. 避免eclipse下启动run就进入debug模式
  11. c语言程序设计怎么改卷,C语言程序设计(B卷)教程.doc
  12. Oracle autotrace使用说明
  13. 基于C#的安全聊天工具设计
  14. QT中的.pro文件,以及.pri .prj .prl文件说明
  15. 铂电阻测温电路c语言程序,pt100测温电路(经典测温范围)
  16. 武汉同济医院挂号指南
  17. 照片加水印怎么弄?方法详细介绍
  18. 如何在苹果Mac OS系统中安装MT4电脑版软件?
  19. visio常用快捷键_Visio快捷键
  20. 词汇课程——词的颜色与易混淆的词缀(4)

热门文章

  1. 洛阳地铁一号线无人驾驶_刚刚,最新消息!涉及洛阳地铁1号线、2号线…
  2. 【clickhouse】yandex 官方 BalancedClickhouseDataSource 源码分析
  3. 【Elasticsearch】使用 Elasticsearch Painless 脚本以递归方式遍历 JSON 字段
  4. 【Elasticsearch】java 客户端 获取 termvectors 词频 统计
  5. 【clickhouse】clickhouse 利用Grafana与系统表监控ClickHouse查询
  6. 【Flink】TableException: A raw type backed by type information has no serializable
  7. Spark Streaming自定义接收器
  8. spark学习-67-源代码:schedulerBackend和taskScheduler的总结
  9. Spring : Spring自定义FactoryBean
  10. Kylin 2.6.0JDBC方式访问