【WebService】——CXF整合Spring
相关博客: 【WebService】——入门实例 【WebService】——SOAP、WSDL和UDDI 前言: ? ? ? 之前的几篇博客基本上都是使用jdk来实现WebService的调用,没有使用任何框架的东西。这样的操作对WebService客户端、服务端的调用、生成等关系的理解会比较深刻,当然也有很大的弊端,需要手动拷贝生成的代码,进行大量重复性的工作。这时候就需要引入框架了,本篇博客介绍其中一种框架CXF,他可以和spring整合实现WebService。 1、下载CXF? ? ? ?apache-cxf-2.5.0 ? ? ? ?解压之后可以看到它的目录结构,其中bin中主要是一些bat文件或命令,后面会经常用到的就是wsdl2java。lib中是一系列的jar包。因为cxf支持spring,自带的lib中有一些spring的核心包。 2、环境变量在Path的值后加“;E:softwareapache-cxf-2.5.0bin” 注意:加上;分号 测试是否配置成功: cd E:softwareapache-cxf-2.5.0bin e: wsdl2java 如果出现如下信息,则说明已经可以找到该命令,环境变量配置成功。 3、新建项目(服务端)引入jar包: 将cxf解压文件的lib目录下的jar包加到项目中。 与前几篇博客相同,我们在服务端给出一个接口IHelloWorld和实现类HelloWorldImpl。 IHelloWorld接口的方法为: @WebService public interface IHelloWorld { public String sayHello(String text); } 实现类HelloWorldImpl。 @WebService(endpointInterface="cn.com.service.IHelloWorld") public class HelloWorldImpl implements IHelloWorld { public String sayHello(String text) { return "Hello" + text ; } } 配置文件 1)applicationContext.xml ? ? ?在src下新建该文件,其中import标签引入的三个配置文件在cxf的核心jar包下,具体路径:cxf-2.5.0.jarMETA-INFcxf。 <?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> 2)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>/WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <!-- 该listener保证 在web应用启动时加载spring容器 --> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--所有来自/*的请求,都交由CXFServlet处理 --> <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> </web-app> 发布服务 ? ? ? 将项目部署到tomcat上,启动后在浏览器地址输入http://localhost:8080/webws/HelloWorld?wsdl (webws为项目名称)。
<wsdl:definitions name="HelloWorldImplService" targetNamespace="http://service.com.cn/"> <wsdl:portType name="HelloWorldImpl"></wsdl:portType> <wsdl:binding name="HelloWorldImplServiceSoapBinding" type="tns:HelloWorldImpl"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> </wsdl:binding> <wsdl:service name="HelloWorldImplService"> <wsdl:port binding="tns:HelloWorldImplServiceSoapBinding" name="HelloWorldImplPort"> <soap:address location="http://localhost:8080/webws/HelloWorld"/> </wsdl:port> </wsdl:service> </wsdl:definitions> 4、生成客户端新建一个空的客户端项目,进入src目录,使用wsdl2java生成客户端。 Refresh刷新项目,可以看到,客户端项目中生成的相应的文件,效果和使用wsimport命令相同。 测试: 在客户端新建测试类,注意在测试的时候要保证tomcat上部署好服务。
public static void main(String[] args) { HelloWorldImplService factory=new HelloWorldImplService(); IHelloWorld hw=factory.getHelloWorldImplPort(); System.out.println(hw.sayHello("Sherry")); } ? ? ? 以上就是cxf整合spring实现WebService调用的例子。小编认为,cxf框架实现WebService的亮点有两个:一是将生成客户端的命令进行了封装,省去了用jdk开发时复杂的命令输入和繁琐的文件拷贝工作。二是整合spring,spring框架的优点就不用多说了,大大提高了开发的效率和可扩展性。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |