环境:CXF 2.6.3
IDE: eclipse for javaEE JUNO
这系列文章都是看书看视频的笔记 如果有错误欢迎指正^_^
过程是比较简单的。CXF的程序用现成的 不讲如何开发JAX-WS或者JAX-RS
1.创建项目(动态web项目)在 web-inf/下导入CXF和其他第三方包
2.修改web.xml 增加spring配置:
在web.xml中增加启动spring的配置和CXF的有关配置
配置内容如下:
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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"> <display-name>spring_cxf</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/beans-server.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <servlet> <servlet-name>CXFServlet</servlet-name> <servlet-class> org.apache.cxf.transport.servlet.CXFServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/ws/*</url-pattern> </servlet-mapping> </web-app>
beans-server.xml是指定的spring配置文件(具体解释可以看:http://blog.csdn.net/sapphire_aling/article/details/6069764)
beans-server.xml的内容如下,在spring的配置中增加CXF的内容:
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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"> <!-- 导入 CXF 扩充XML标记库,用于在Spring启用WebService标记 --> <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" /> <!-- CXF 提供的内置拦截器 --> <bean id="inLoggingInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor" /> <bean id="outLoggingInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor" /> <!-- 配置方案一 使用JAX-WS标准配置jaxws:endpoint,发布服务 --> <!-- id:spring bean标识,implementor:服务实现类,address:服务发布路径 --> <jaxws:endpoint id="merchant_1" implementor="com.cxfdemo.server.service.impl.IMerchantServiceImpl" address="/m_1"> <!-- 可选配置入口拦截器 --> <jaxws:inInterceptors> <ref bean="inLoggingInterceptor" /> </jaxws:inInterceptors> <!-- 可选配置出口拦截器 --> <jaxws:outInterceptors> <ref bean="outLoggingInterceptor" /> </jaxws:outInterceptors> </jaxws:endpoint> <!-- 配置方案二 使用JAX-WS标准配置jaxws:endpoint,发布服务 --> <!-- id:spring bean标识,serviceClass:服务实现接口,address:服务发布路径 --> <jaxws:server id="merchant_2" serviceClass="com.cxfdemo.server.service.IMerchantService" address="/m_2"> <!-- 注入:服务实现类 --> <jaxws:serviceBean> <ref bean="merchantService"/> </jaxws:serviceBean> <!-- 可选配置入口拦截器 --> <jaxws:inInterceptors> <ref bean="inLoggingInterceptor" /> </jaxws:inInterceptors> <!-- 可选配置出口拦截器 --> <jaxws:outInterceptors> <ref bean="outLoggingInterceptor" /> </jaxws:outInterceptors> </jaxws:server> <!-- 服务实现类 --> <bean id="merchantService" class="com.cxfdemo.server.service.impl.IMerchantServiceImpl" /> </beans>
以上有两种方式
- 使用jaxws:endpoint 和代码里使用EndPoint是差不多的 配置服务实现类和地址即可(也可以增加拦截器等;
- jaxws:server 方式 这和JaxWsServerFactoryBean的使用差不多 配置服务类配置地址 还有服务的实现类
以上完成就可以运行了:
可见运行正常
JAX-RS同理 只需要修改spring配置文件即可:
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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"> <!-- 导入 CXF 扩充XML标记库,用于在Spring启用WebService标记 --> <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" /> <!-- CXF 提供的内置拦截器 --> <bean id="inLoggingInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor" /> <bean id="outLoggingInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor" /> <jaxrs:server id="merchantrs" address="/"> <jaxrs:serviceBeans> <ref bean="merchantService"/> </jaxrs:serviceBeans> <jaxrs:extensionMappings> <entry key="json" value="application/json" /> <entry key="xml" value="application/xml" /> </jaxrs:extensionMappings> <!-- 可选配置入口拦截器 --> <jaxrs:inInterceptors> <ref bean="inLoggingInterceptor" /> </jaxrs:inInterceptors> <!-- 可选配置出口拦截器 --> <jaxrs:outInterceptors> <ref bean="outLoggingInterceptor" /> </jaxrs:outInterceptors> </jaxrs:server> <!-- 服务实现类 --> <bean id="merchantService" class="com.cxfdemo.server.service.impl.IMerchantServiceImpl" /> </beans>
以上的代码来自北风网的视频课程的源代码(太懒了就没自己重写个webservice) 这里以它的代码来做笔记用(不是广告的说..哎~)
相关推荐
CXF WebService整合Spring示例工程代码demo可以直接导入eclipse。参照网页http://www.cnblogs.com/hoojo/archive/2011/03/30/1999563.html 完成的webService服务提供。 大致步骤: 1.引入cxf和其他需要的jar包,(本...
使用spring集成cxf,在两个web project里发布及调用webservice server端使用spring+springmvc+mybatis+cxf,client端使用struts2+spring+hibernate+cxf 两个工程均为myeclipse project,包含所有除myeclipse自带以外...
用于webservice的开发,主要用到spring+cxf的方式实现webservice的发布与简单调用测试,不懂得可以留言给我,比较显新的框架配置,希望你会喜欢!(并包含拦截器的配置!)
这个是cxf 集成spring 的webservice的例子 供大家参考
spring+cxf 集成 实现webservice
NULL 博文链接:https://chilongxph.iteye.com/blog/510707
基于spring和CXF发布webservice,包含java本地发布和调用及web发布和调用。
Spring集成Cxf调用WebServices
Cxf集成Spring的WebService接口的配置文档
使用最新的CXF2.7和Spring3的 Java WebService 集成用例 应用Spring Annotation
webService实现 cxf2.7.5 与 spring3.0集成 完整流程,内涵全部jar ,下载集合运行
cxf与spring整合webservice所需jar
SpringBoot WebService cxf接口发布以及logbok日志集成, 希望对学习的小伙伴们有帮助
readme下为开发文档。 1.集成 spring 2.集成 WSS4J 3.mtom 功能示例 4.基本数据类型service接口 示例 5.复杂数据类型service接口 示例
cxf集成Spring的restful WebServ ice接口,Demo的完整代码!
CXF集成Spring发布WebService代码实例
cxf集成Spring的restful WebService接口以Json形式表现的完整代码,适合新入门在学WebService的小伙伴们参考,希望对你们有帮助!
有大量简单的 API 用来快速地构建代码优先的 Services,各种 Maven 的插件也使集成更加容易,支持 JAX-WS API ,支持 Spring 2.0 更加简化的 XML 配置方式,等等。 4. 支持二进制和遗留协议:CXF 的设计是一种可插...
springboot2.1.5集成CXF3.2.5,webservice服务端,亲测可用
webService CXF不依赖Spring集成,包含客户端、服务端代码。使用3.1.7版的CXF包。demo可以直接运行