WebService学习总结(六)--CXF 与Spring结合+tomcat发布
该项目在上文 WebService学习总结(四)--基于CXF的服务端开发 的基础上修改为spring上发布的webservice接口
1、新建web project 工程
2、导入spring和cxf的有关jar包
3、 在src 目录下,配置sping 的配置文件
4、在src 目录,新建一个sping的配置文件applicationContext-server.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd"> <!-- 配置自动扫描的包 -->
<!--
<context:component-scan base-package="com.service.springmvc"></context:component-scan> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean> <bean class="org.springframework.remoting.jaxws.SimpleJaxWsServiceExporter">
<property name="baseAddress" value="http://localhost:8085/"/>
</bean>
-->
<!-- 此次在这里配置,可以设置配置文件
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
-->
<bean id="studentWsImpl" class="com.myl.service.serviceImpl.StudentWsImpl"></bean> <jaxws:server id="studentWsSpring" serviceClass="com.myl.service.StudentWs" address="/StudentSpringWs">
<jaxws:serviceBean>
<ref bean="studentWsImpl"></ref>
</jaxws:serviceBean>
</jaxws:server> </beans>
这里xml 配置文件里面, bean id 指定了web service 接口实现类,也就是调用接口后实际业务逻辑的实现类; server id 和address 是 web service接口暴露的地址,对应的是接口类 StudentWs, 而这个服务的serviceBean 指定为接口实现类,这样就指定了运行时调用的实现类。 这个配置也就是sping核心的ioc控制反转功能,通过配置文件将依赖的对象,用配置文件来实现依赖注入,从而实现控制反转。
5、修改web.xml 文件
<?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" version="3.1">
<display-name>SpringCXF</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
</servlet>
<servlet>
<servlet-name>cxfService</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>cxfService</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
context-param里面指定了spring 配置文件
6、发布到tomcat里面
7、 运行tomcat,如下图所示在表示运行成功

8、查看发布的服务
通过下面url ,来查看发布的服务:http://localhost:8080/SpringCXF/service/ 这里StudyWsSpringCXF是工程项目的名称,如果一切正常,打开的界面如下:

9、可以点击红框中内容获取wsdl 文档。

10、通过客户端验证
运行之前的客户端,验证一下发布的服务端能正常工作,需要注意的是,需要修改客户端类StudentWsClient.java里面调用服务端的地址,与当前发布的服务端一致
jwpfb.setAddress("http://localhost:8080/SpringCXF/service/StudentSpringWs");
package com.client; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import com.webservice.StudentWs; /**
*
* @author myl
* @date 2018年4月18日 上午2:44:26
* 客户端调用服务端发布的接口
*/
public class StudentSerivceTest { public static void main(String[] args) {
//获取工厂对象
JaxWsProxyFactoryBean jwpfb = new JaxWsProxyFactoryBean();
//加载生成的StudentWsImpl类
jwpfb.setServiceClass(StudentWs.class);
//传入url接口地址
jwpfb.setAddress("http://localhost:8080/SpringCXF/service/StudentSpringWs");
//创建接口对象
StudentWs ws = (StudentWs) jwpfb.create();
//调用接口中的方法
ws.addStudentService("mao", "11", "22");
ws.addStudentService("ya", "15", "23");
ws.queryStudentService("mao");
ws.queryStudentService("ya"); } }
运行客户端输出结果:

总结
通过sping+tomcat+cxf 发布webservice 服务的步骤如下:
1、建立工程、编写web service 服务端相应的代码,实现业务逻辑
2、编写sping的配置文件 applicationContext.xml 文件,这个文件放在src 的根目录下
3、编写配置web.xml 文件
4、发布到tomcat中、并进行调试
WebService学习总结(六)--CXF 与Spring结合+tomcat发布的更多相关文章
- Webservice实践(七)CXF 与Spring结合+tomcat发布
上一节介绍了如何使用CXF 来发布服务,但是没有介绍使用web 容器来发布,很多项目需要用tomcat 这样的容器来发布.另外本节将介绍CXF 与spring 结合的方法. 一 目标: 1.利用spi ...
- WebService—CXF整合Spring实现接口发布和调用过程
一.CXF整合Spring实现接口发布 发布过程如下: 1.引入jar包(基于maven管理) <!-- cxf --> <dependency> <groupId> ...
- 使用Spring和Tomcat发布CXF REST WebService
与发布SOAP WS一样, 同样需要在web.xml文件中配置CXFServlet: <!--cxf的Servlet--> <servlet> <servlet-name ...
- 使用Spring和Tomcat发布CXF SOAP WebService
上一节中使用代理工厂JaxWsProxyFactoryBean来发布WebService, 这种方式必须指定运行的端口,如果端口被占用,就会发布失败. cxf的WebService也可利用Tomcat ...
- WebService -- Java 实现之 CXF ( 使用Spring添加拦截器)
最重要的就是在ApplicationContext.xml下面添加配置 <!-- service provider --> <jaxws:endpoint implementor=& ...
- Docker学习(六)-Kubernetes - Spring Boot 应用
接上一篇 https://www.cnblogs.com/woxpp/p/11872155.html 新建 k8s-demo.yaml apiVersion: apps/v1beta2 kind: D ...
- WebService学习之旅(一)使用JAX-WS发布WebService
JAX-WS全称Java™ API for XML Web Services,是随着JDK1.6及其后续版本发布的方便Java程序员开发WebService应用的一组API,通常简称为JWS,目前版本 ...
- maven版cxf集合spring开发服务端(二)
一.新建一个maven项目 二.pom.xml引入依赖 <dependency> <groupId>org.apache.cxf</groupId> <art ...
- WebService学习之三:spring+cxf整合
步骤一:spring项目(java web项目)引入CXF jar包 步骤二:创建webservice服务器 1)创建一个服务接口 package com.buss.app.login; import ...
随机推荐
- ifix与4G DTU对接实现数据显示
前言: 因公司项目需求,需要使用4G DTU设备对远端RS 485设备进行数据采集,购买了相关产品进行技术实验,成功对接ifix将数据采集并显示,将过程记录,供大家参考. 1,4G DTU基本原理和配 ...
- videojs文档翻译-api
直播流地址 rtmp://live.hkstv.hk.lxdns.com/live/hks API 接口 (一) Modules 模块 1) browser :浏览器 2) ...
- 《Android原生整合虹软SDK开发uniapp插件》
1.项目背景 应公司要求,需要开发一套类似人脸打卡功能的app,但是因为我们公司没有很强的原生android开发者,所以根据现状选择了第三方跨平台的uniapp,想必目前大多人都了解这个平台了,我也就 ...
- Python 机器学习实战 —— 无监督学习(下)
前言 在上篇< Python 机器学习实战 -- 无监督学习(上)>介绍了数据集变换中最常见的 PCA 主成分分析.NMF 非负矩阵分解等无监督模型,举例说明使用使用非监督模型对多维度特征 ...
- MarkDown语法(Typora软件为例)
Hello !我又来了 这篇文章主要给大家讲一下MarkDown的一些基础语法,MarkDown语法是一种轻量级标记语言,它允许人们使用易读易写的纯文本格式编写文档,然后转换成有效的XHTML( ...
- Cell Reports | 上海瑞金医院糜坚青等揭示组蛋白酰化/乙酰化修饰比率调控BRD4基因组分布
景杰生物 | 报道 组蛋白翻译后修饰,被认为构成一类超越基因序列的"组蛋白密码",控制着遗传信息的组织层次及其在染色质层面的解读.组蛋白赖氨酸乙酰化是研究最早的一类组蛋白修饰, ...
- Java代码搭建Dubbo+ZooKeeper 的示例
.personSunflowerP { background: rgba(51, 153, 0, 0.66); border-bottom: 1px solid rgba(0, 102, 0, 1); ...
- DL基础补全计划(六)---卷积和池化
PS:要转载请注明出处,本人版权所有. PS: 这个只是基于<我自己>的理解, 如果和你的原则及想法相冲突,请谅解,勿喷. 前置说明 本文作为本人csdn blog的主站的备份.(Bl ...
- Python入门:ChainMap 有效管理多个上下文
摘要: Python的ChainMap从collections模块提供用于管理多个词典作为单个的有效工具. 本文分享自华为云社区<从零开始学python | ChainMap 有效管理多个上下文 ...
- 软件开发目录规范 ATM框架构建
软件开发的目录规范 建立文件夹 为了提高程序的可读性与可维护性,我们应该为软件设计良好的目录结构,这与规范的编码风格同等重要.软件的目录规范并无硬性标准,只要清晰可读即可 以ATM购物车项目为例: 首 ...