近些年来,Spring一直很火,许多框架都能跟Spring完美集成,CXF也不例外。下面,我就介绍一下如何使用CXF + Spring发布webService。我们还是使用前两篇博客使用的实例。
服务端:
目录结构:
这里需要的所有Spring的包,CXF的lib目录下都有。
IHelloWorldServer代码:
package com.test.server; import javax.jws.WebService; @WebService public interface IHelloWorldServer { public String sayHello(String username); }
HelloWorldServerImp代码:
package com.test.server; import javax.jws.WebService; @WebService(endpointInterface="com.test.server.IHelloWorldServer",serviceName="HelloService") public class HelloWorldServerImp implements IHelloWorldServer { @Override public String sayHello(String username) { return username+" : HelloWorld"; } }
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:jaxws="http://cxf.apache.org/jaxws" 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"> <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" /> <jaxws:endpoint id="helloService" implementor="com.test.server.HelloWorldServerImp" address="/helloService" /> </beans>
web.xml
<?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_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>cxf_demo</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <display-name>cxf_demo</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext-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>
其实这种方式与我们手动写Servlet并没有本质的区别,只不过这里让Spring代劳罢了。
以上就是服务端的代码,直接发布项目即可。发布完成后,输入网址:http://localhost:8080/cxf_demo1/ws 查看发布的WebService。
客户端:
这次我们不采用动态客户端,而是引用服务的接口实现。
目录结构:
红色方框中的jar就是我们用服务端的接口生成的jar。
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:jaxws="http://cxf.apache.org/jaxws" 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"> <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="client" class="com.test.server.IHelloWorldServer" factory-bean="clientFactory" factory-method="create" /> <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean"> <property name="serviceClass" value="com.test.server.IHelloWorldServer" /> <property name="address" value="http://localhost:8080/cxf_demo1/ws/helloService" /> <!-- value="http://localhost:8080/cxf_demo_noSpring_1/ws/helloWorld" /> --> </bean> </beans>HelloWorldClient代码:
package com.test.client; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.test.server.IHelloWorldServer; public class HelloWorldClient { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext-client.xml"); IHelloWorldServer helloService = (IHelloWorldServer) context .getBean("client"); String response = helloService.sayHello("haitao"); System.out.println(response); } }
run后结果如下:
到这里,Spring+CXF发布WebService完美实现。
我们可以看到,这次客户端实现没有用动态方式,这样就必须要用到服务端的接口jar包,这样的实现效率要比动态方式高,不过需要引用服务端jar包有时候是让人无法接受的。
关于CXF,我就先介绍这么多,学会这些只能说关于WebService你才刚刚入门,更多东西还有待于进一步研究、发现。