项目中要使用cxf,参考网上资料做的一个cxf+spring的实例。
一、所需要的jar:
二、在web.xml填加以下配置
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- 设置Spring容器加载配置文件路径 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext-server.xml</param-value> </context-param> <!-- 加载Spring容器配置 --> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <!-- 配置cxf的核心控制器 --> <servlet> <servlet-name>CXFServlet</servlet-name> <servlet-class> org.apache.cxf.transport.servlet.CXFServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <!-- 所有来自/webservice/*的请求交给cxf处理 --> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/webservice/*</url-pattern> </servlet-mapping> </web-app>
三、创建服务端webservice类
1、首先定制服务器端的接口
package com.ws.cxf.dao; import javax.jws.WebMethod; import javax.jws.WebService; @WebService public interface IHelloWorld { @WebMethod public String sayHello(String name); }
2、服务器端的接口的实现类
package com.ws.cxf.daoImpl; import com.ws.cxf.dao.IHelloWorld; public class HelloWorldImpl implements IHelloWorld{ public String sayHello(String name) { System.out.println("sayHello() is called"); return name +" helloWorld"; } }
四、创建spring配置文件
1、创建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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 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-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <!--CXF配置 --> <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" /> <!--服务端发布的webservice 在spring中使用jaxws:endpoint元素来暴露Web Service id:指在spring配置的bean的ID Implementor:指明具体的实现类 Address:指明这个web service的相对地址 --> <jaxws:endpoint id="helloWorld" implementor="com.ws.cxf.daoImpl.HelloWorldImpl" address="/HelloWorld" /> </beans>
2、创建applicationContext-client.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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 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-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <!--CXF配置 --> <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" /> <!--客户端调用的webservice --> <jaxws:client id="helloWorldClient" address="http://localhost:8080/cxfTest/webservice/HelloWorld" serviceClass="com.ws.cxf.dao.IHelloWorld" /> </beans>
五、服务端测试类
package com.ws.cxf.client; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.ws.cxf.dao.IHelloWorld; public class Client { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-client.xml"); IHelloWorld helloWorld=(IHelloWorld)context.getBean("helloWorldClient"); System.out.println(helloWorld.sayHello("jim")); } }
六、测试
1、访问以下地址验证服务端是否配制成功
http://localhost:8080/web工程名/webservice/HelloWorld?wsdl 查看是否发布成功并生成了wsdl文件
http://localhost:8080/web工程名/webservice 直接访问该地址可查看系统提供几个webservice服务
2、运行Client类输出 jim helloWorld 则客户端连接成功。
源代码:
相关推荐
在本文中,我们将深入探讨如何将Apache CXF 2.7.3与Spring 3.0.7框架整合进行开发。Apache CXF是一个开源的Java框架,主要用于构建和部署SOAP和RESTful Web服务,而Spring则是一个广泛使用的应用框架,提供了依赖...
这个名为"apache-cxf-2.7.3.rar"的压缩包包含了CXF框架的2.7.3版本,这是一个广泛使用的版本,提供了丰富的功能和良好的社区支持。在这个版本中,你可以找到用于创建、部署和消费Web服务的各种工具和库。 CXF的主要...
"apache-cxf-2.7.3"是该框架的一个特定版本,发布于2012年,提供了一系列功能和改进,以帮助开发者在Web服务领域更高效地工作。 Apache CXF的核心特性包括: 1. **服务实现**:CXF允许开发者使用Java编程模型来...
赠送jar包:cxf-core-3.0.1.jar; 赠送原API文档:cxf-core-3.0.1-javadoc.jar; 赠送源代码:cxf-core-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-core-3.0.1.pom; 包含翻译后的API文档:cxf-core-3.0.1-...
赠送jar包:cxf-rt-rs-client-3.0.1.jar; 赠送原API文档:cxf-rt-rs-client-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-rs-client-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-rt-rs-client-3.0.1.pom; 包含...
jar包,亲测可用
- `cxf-rt-frontend-jaxws.jar`:JAX-WS前端模块,用于创建和消费SOAP服务。 - `cxf-rt-frontend-jaxrs.jar`:JAX-RS前端模块,用于RESTful服务的开发。 - `cxf-rt-transports-http.jar`和`cxf-rt-transports-...
赠送jar包:cxf-rt-frontend-jaxrs-3.0.1.jar; 赠送原API文档:cxf-rt-frontend-jaxrs-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-frontend-jaxrs-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-rt-frontend-jaxrs...
jar包,亲测可用
jar包,亲测可用
jar包,亲测可用
赠送jar包:cxf-rt-transports-http-3.0.1.jar; 赠送原API文档:cxf-rt-transports-http-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-transports-http-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-rt-transports-...
赠送jar包:cxf-rt-frontend-simple-3.0.1.jar; 赠送原API文档:cxf-rt-frontend-simple-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-frontend-simple-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-rt-frontend-...
赠送jar包:cxf-rt-frontend-jaxws-3.0.1.jar; 赠送原API文档:cxf-rt-frontend-jaxws-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-frontend-jaxws-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-rt-frontend-jaxws...
赠送jar包:cxf-rt-bindings-soap-3.0.1.jar; 赠送原API文档:cxf-rt-bindings-soap-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-bindings-soap-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-rt-bindings-soap-...
6. **Spring集成**:CXF与Spring框架深度集成,使得服务的配置和管理变得更加方便,同时也易于与其他Spring生态系统的组件配合使用。 7. **安全性**:CXF提供了多种安全机制,如基本认证、Digest认证、OAuth、SSL/...
这个"apache-cxf-2.5.2"版本是该框架的一个特定发行版,发布于2011年,包含了CXF框架的所有组件和依赖项,供开发者在他们的项目中使用。 Apache CXF的主要特性包括: 1. **Web服务实现**:CXF允许开发者使用JAX-WS...
neethi-3.0.2.jar,spring-asm-3.0.7.RELEASE.jar,spring-asm-3.0.7.RELEASE.jar,spring-beans-3.0.7.RELEASE.jar,spring-context-3.0.7.RELEASE.jar,spring-core-3.0.7.RELEASE.jar,spring-expression-3.0.7....
这个"apache-cxf-2.2.4"版本是该框架的一个历史版本,发布于2009年,提供了对SOAP、RESTful、WS-*等标准的支持。 CXF的名称来源于两个它合并的项目——Celtix和XFire,这两个项目都是用于构建Web服务的工具。CXF的...