本来都是WebAPI/RestfulAPI接口对外提供接口的,突然有个需求要提供WebService接口,本想着Spring和CXF这么成熟的两个产品,这么的也整不出什么幺蛾子来啊。

结果还真是出了几个幺蛾子。把解决方案贴出来,希望能帮助有需求的小伙伴少走弯路。

解决方案的软件版本(OpenJDK10,Spring MVC 5,CXF 3)

1.OpenJDK中WebService的注解不见了

解决方案:追加第三方依赖包,注意type是pom

        <dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-ri</artifactId>
<version>2.3.2</version>
<type>pom</type>
</dependency>

<dependency>
        <groupId>com.sun.activation</groupId>
        <artifactId>javax.activation</artifactId>
        <version>1.2.0</version>
    </dependency>

2.WebService配置文件,网上搜出来的版本五花八门,不知道哪种是对的

解决方案:

spring-webservice.xml

<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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<!-- 自动扫描webService -->
<context:component-scan base-package="com.xxx.webservice.impl" />
<!-- 定义webservice的发布接口 -->
<jaxws:endpoint
implementor="#helloWorld"
address="/HelloWorld"></jaxws:endpoint>
</beans>

pom.xml

        <!-- webservice -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.8</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.8</version>
</dependency>

3.按照网上的方案,此刻追加了webservice的接口和实现类,

在web.xml里面配置一下映射应该就可以了,

    <servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-*.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--定义一个cxf的servlet-->
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/webservice/*</url-pattern>
</servlet-mapping>

但是,这时候会出现“Can't find the the request for http://localhost:8080/xxx/webservice/HelloWorld's Observer ”找不到实现类

这时候涉及到Spring的两个概念ContextLoaderListener和DispatcherServlet

简单来说ContextLoaderListener是全局Spring的容器,DispatcherServlet是Spring MVC的容器,作用域是不一样的。

我们在SpringMVC中,通常只用到了DispatcherServlet,而CXF目前来看不是使用的这个容器的上下文,所以找不到实现类。

于是我们在web.xml中追加了ContextLoaderListener,并且把所有的Spring相关配置都设置在了他的上下文

    <listener>
<description>Define spring listener.</description>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <context-param>
<description>Define applicationContext.xml location.</description>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring/spring-*.xml
</param-value>
</context-param>
<servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--定义一个cxf的servlet-->
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/webservice/*</url-pattern>
</servlet-mapping>

这时候,就可以跑通了,调用到webservice接口了

Spring MVC + CXF实现webservice接口的更多相关文章

  1. spring集成cxf实现webservice接口功能

    由于cxf的web项目已经集成了Spring,所以cxf的服务类都是在spring的配置文件中完成的.以下是步骤:第一步:建立一个web项目.第二步:准备所有jar包.将cxf_home\lib项目下 ...

  2. struts1+spring+myeclipse +cxf 开发webservice以及普通java应用调用webservice的实例

    Cxf + Spring+ myeclipse+ cxf 进行  Webservice服务端开发 使用Cxf开发webservice的服务端项目结构 Spring配置文件applicationCont ...

  3. Spring Boot+CXF搭建WebService(转)

    概述 最近项目用到在Spring boot下搭建WebService服务,对Java语言下的WebService了解甚少,而今抽个时间查阅资料整理下Spring Boot结合CXF打架WebServi ...

  4. Spring集成CXF发布WebService并在客户端调用

    Spring集成CXF发布WebService 1.导入jar包 因为官方下载的包里面有其他版本的sprring包,全导入会产生版本冲突,所以去掉spring的部分,然后在项目根目录下新建了一个CXF ...

  5. 使用cxf开发webservice接口

    项目中经常用到开发webservice接口,及调用webService接口.这里讲解如何使用cxf开发webService接口. 一.webservice介绍及理解 webservice是一种跨平台, ...

  6. spring mvc + mybaties + mysql 完美整合cxf 实现webservice接口 (服务端、客户端)

    spring-3.1.2.cxf-3.1.3.mybaties.mysql 整合实现webservice需要的完整jar文件 地址:http://download.csdn.net/detail/xu ...

  7. Spring boot+CXF开发WebService

    最近工作中需要用到webservice,而且结合spring boot进行开发,参照了一些网上的资料,配置过程中出现的了一些问题,于是写了这篇博客,记录一下我这次spring boot+cxf开发的w ...

  8. Spring boot+CXF开发WebService Demo

    最近工作中需要用到webservice,而且结合spring boot进行开发,参照了一些网上的资料,配置过程中出现的了一些问题,于是写了这篇博客,记录一下我这次spring boot+cxf开发的w ...

  9. Spring 组cxf宣布webservice

    通过spring宣布webservice接口 spring jar包+cxf jar Java包 下列文件丢失jar包需要下载自己 项目结构 watermark/2/text/aHR0cDovL2Js ...

随机推荐

  1. 201871010101-陈来弟《面向对象程序设计(Java)》第十二周学习总结

    201871010101-陈来弟<面向对象程序设计(Java)>第十二周学习总结 实验十   集合与GUI初步 实验时间 2019-11-14 第一部分   理论部分 1.(1) 用户界面 ...

  2. 201871010133-赵永军《面向对象程序设计(java)》第十一周学习总结

    201871010133-赵永军<面向对象程序设计(java)>第十一周学习总结 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ ...

  3. Mysql8.0中caching_sha2_password报错解决

    https://blog.csdn.net/litte_frog/article/details/80874105

  4. Python进阶-XI 常用模块之一:collections、time、random、os、sys

    简要介绍一下各种集合: 列表.元组.字典.集合(含frozenset).字符串.堆栈(如手枪弹夹:先进后出).队列(如马克沁机枪的弹夹:先进先出) 1.collections 1)queue 队列介绍 ...

  5. <Tree> 110 124

    110. Balanced Binary Tree 方法是如果我们发现子树不平衡,则不计算具体的深度,而是直接返回-1.那么优化后的方法为:对于每一个节点,我们通过checkDepth方法递归获得左右 ...

  6. B1043 输出PATest (20 分)

    一.技术总结: 对于哈希字符处理方式,一般是用一个数组存储字符出现的次数,然后再考虑后续. 同时,在输出时,比如这题要输出指定几个字符,我们可以首先统计下这几个字符一共出现的次数sum,然后输出一个就 ...

  7. [LeetCode] 22. Generate Parentheses 生成括号

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  8. 用 cgroups 管理 cpu 资源

    转自:http://xiezhenye.com/2013/10/用-cgroups-管理-cpu-资源.html 这回说说怎样通过 cgroups 来管理 cpu 资源.先说控制进程的 cpu 使用. ...

  9. qps.sh

    mysql -p'' -Bse'show global status like "com_%";' > qps.new while true do sleep 0.5 mv ...

  10. 'try(A a = new A())' VS 'try finally'

    实现了AutoCloseable接口的类,可以在try的时候直接实例化对象.try代码块完成之后,自动调用close方法,相当于在finally里主动调用.但是出现异常后的流程和try finally ...