环境:JDK6.0,cxf-2.0.7,spring2.5,tomcat6.0
服务器端:
1.新建Web项目,例如webws,导入cxf-2.0.7/lib下的jar包和spring2.5的包,因为cxf支持spring,因此它自带有sping的一些核心包,为了以后扩展,保险起见都一起导入吧。。
2.新建一个服务接口IHelloWorld.java,该接口是面向客户端,暴露服务:
package cn.com.service; import javax.jws.WebParam; import javax.jws.WebService; @WebService public interface IHelloWorld { //@WebParam(name="arg0")可有可无,为了增强可读性 public String sayHello(@WebParam(name="arg0")String text); }
3.再新建该接口的实现类HelloWorldImpl.java:
package cn.com.service; import javax.jws.WebService; @WebService(endpointInterface="cn.com.service.IHelloWorld") public class HelloWorldImpl implements IHelloWorld { public String sayHello(String text) { return "Hello" + text ; } }
4.现在可以进行spring配置了,在eclipse的src文件夹下新建applicationContext.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:jaxws="http://cxf.apache.org/jaxws" xmlns:cxf="http://cxf.apache.org/core" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <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="hello" class="cn.com.service.HelloWorldImpl"/> <jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" /> </beans>
注意:1).<beans>元素里面的命名空间一定要正确,特别要增加xmlns:jaxws="http://cxf.apache.org/jaxws",http://cxf.apache.org/jaxws,http://cxf.apache.org/schemas/jaxws.xsd;
2).cxf.xml,cxf-extension-soap.xml,cxf-servlet.xml三个文件都在cxf-2.0.7.jar中把它们拷贝到META-INF/目录下。
5.配置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"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>CXFServlet</servlet-name> <display-name>CXF Servlet</display-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>/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
到此服务器端基本上告一段落,可以将应用部署到tomcat,启动并访问http://localhost:8080/webws/HelloWorld?wsdl,如果能正确显示xml文件则说明部署成功。
客户端:
1.新建一个java项目,例如wsclient,也导入cxf-2.0.7和spring包
2.新建客户程序(如果熟悉cxf可以运用cxf的wsdl2java命令直接根据wsdl生成),如Test.java:
package pro.webws.client; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-client.xml"); IHelloWorld client = (IHelloWorld) ctx.getBean("client"); String result = client.sayHello("你好!"); System.out.println(result); } }
根据代码,我们知道还需要新建一个IHelloWorld接口,这是服务器端暴露的服务,我们还需要在客户端再新建一个以利用之,代码和服务器端一致即可,不再赘述。
现在可以配置客户端的spring文件了,新建spring-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:p="http://www.springframework.org/schema/p" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:cxf="http://cxf.apache.org/core" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd"> <bean id="client" class="pro.webws.client.IHelloWorld" factory-bean="clientFactory" factory-method="create" /> <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean"> <property name="serviceClass" value="pro.webws.client.IHelloWorld" /> <property name="address" value="http://localhost:8080/webws/HelloWorld" /> </bean> </beans>
注意:<property name="address" value="http://localhost:8080/webws/HelloWorld" />中的/HelloWorld要与服务器端applicationContext.xml中的<jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" />的address属性对应。
现在服务器和客户端都已完成,启动tomcat,运行客户程序,将输出结果:
Hello你好!
1 楼
youfengkai
2011-01-17
非常入门级,易懂
2 楼
riapgypm
2011-11-30
不错,谢谢啦
3 楼
liuxueping1987
2012-04-20
log4j:WARN No appenders could be found for logger (org.apache.cxf.bus.spring.BusApplicationContext).
log4j:WARN Please initialize the log4j system properly.
2012-4-20 14:13:49 org.apache.cxf.bus.spring.BusApplicationContext getConfigResources
信息: No cxf.xml configuration file detected, relying on defaults.
2012-4-20 14:13:49 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://client.com.cn/}IHelloWorldService from class cn.com.client.IHelloWorld
2012-4-20 14:13:50 org.apache.cxf.phase.PhaseInterceptorChain doIntercept
信息: Interceptor has thrown exception, unwinding now
org.apache.cxf.interceptor.Fault: Could not send Message.
at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:64)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:221)
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:276)
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:222)
at org.apache.cxf.frontend.ClientProxy.invokeSync(ClientProxy.java:73)
at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:171)
at $Proxy16.sayHello(Unknown Source)
at cn.com.client.TestCXF.main(TestCXF.java:19)
Caused by: java.io.IOException: Not Found
at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponse(HTTPConduit.java:1929)
at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.close(HTTPConduit.java:1832)
at org.apache.cxf.transport.AbstractConduit.close(AbstractConduit.java:66)
at org.apache.cxf.transport.http.HTTPConduit.close(HTTPConduit.java:591)
at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:62)
... 7 more
Exception in thread "main" javax.xml.ws.soap.SOAPFaultException: Could not send Message.
at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:192)
at $Proxy16.sayHello(Unknown Source)
log4j:WARN Please initialize the log4j system properly.
2012-4-20 14:13:49 org.apache.cxf.bus.spring.BusApplicationContext getConfigResources
信息: No cxf.xml configuration file detected, relying on defaults.
2012-4-20 14:13:49 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://client.com.cn/}IHelloWorldService from class cn.com.client.IHelloWorld
2012-4-20 14:13:50 org.apache.cxf.phase.PhaseInterceptorChain doIntercept
信息: Interceptor has thrown exception, unwinding now
org.apache.cxf.interceptor.Fault: Could not send Message.
at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:64)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:221)
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:276)
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:222)
at org.apache.cxf.frontend.ClientProxy.invokeSync(ClientProxy.java:73)
at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:171)
at $Proxy16.sayHello(Unknown Source)
at cn.com.client.TestCXF.main(TestCXF.java:19)
Caused by: java.io.IOException: Not Found
at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponse(HTTPConduit.java:1929)
at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.close(HTTPConduit.java:1832)
at org.apache.cxf.transport.AbstractConduit.close(AbstractConduit.java:66)
at org.apache.cxf.transport.http.HTTPConduit.close(HTTPConduit.java:591)
at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:62)
... 7 more
Exception in thread "main" javax.xml.ws.soap.SOAPFaultException: Could not send Message.
at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:192)
at $Proxy16.sayHello(Unknown Source)
4 楼
liuxueping1987
2012-04-20
按照你的步骤做,服务端没有问题,但是客户端在运行,调用服务时报下面的错误,求指导。。多谢!!
5 楼
liuxueping1987
2012-04-20
已经解决时项目的大小写问题,弄了半天才解决。。。
刚开始我的项目名为SpringAndCXFService 改为 springAndCXFService就ok拉
刚开始我的项目名为SpringAndCXFService 改为 springAndCXFService就ok拉
6 楼
dobuy
2012-07-06
谢谢啊啊啊