项目中要使用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 则客户端连接成功。
源代码:
相关推荐
目录: 一、准备工作(开发环境) 二、创建service接口和实现类 三、配置service服务器 四、配置项目的web.xml文件 五、创建客户端 六、测试
该资源内容为webService 接口的java客户端调用代码生成工具;解压即可使用; 具体操作可查看内附readme
apache cxf web 开发工具版本apache-cxf-2.7.3
jar包,亲测可用
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-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-...
赠送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-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...
<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"/>
apache-cxf-2.5.2
cxf-2.4.2 jar包 cxf-2.4.2 jar包 cxf-2.4.2 jar包
赠送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...
cxf-core-3.1.16版本jar包,用于java开发使用
cxf-rt-frontend-jaxws-3.0.16.jar jar包下载3.0.16版本下载
原地址:http://www.apache.org/dyn/closer.lua/cxf/3.0.7/apache-cxf-3.0.7.zip