用cfx做webservice的简单例子,还有和spring的整合
使用 CXF 做 webservice 简单例子
? Apache CXF 是一个开放源代码框架,提供了用于方便地构建和开发 Web 服务的可靠基础架构。它允许创建高性能和可扩展的服务,您可以将这样的服务部署在 Tomcat 和基于 Spring 的轻量级容器中,以及部署在更高级的服务器上,例如 Jboss、IBM? WebSphere? 或 BEA WebLogic。
? ?????? 该框架提供了以下功能:
????? ?一? 借助 annotation 创建独立启动的web 服务。 ? ???????准备: 新建工程 导入需要的jar 包: ?????????????????? ??????????????????依赖的包: ??????????????????????????? commons-logging-1.1.jar ? ????????????????? spring jar?包, 用来支持xml配置: ??????????????????????????? aopalliance-1.0.jar ?????????????????? ?????????????????? CXF jar包: ??????????????????????????? cxf-2.1.jar ??? ?????????以上jar 包 可从apache官方网站下载?apache-cxf-2.1.2.zip, 然后从apache-cxf-2.1.2/lib 目录中获得 ? ????? 1? 首先服务点接口。 ??????????package com.demo; ? ????????? import java.util.List; ????????? import javax.jws.WebParam; ????????? @WebService ? ???? 2? 编写服务实现 ?????????package com.demo; ???????? import java.util.LinkedHashMap; ???????? import javax.jws.WebService; ? ???????? @WebService(endpointInterface="com.demo.HelloWorld",serviceName="HelloWorld") ? ? ????????????????? ?public String sayHiToUser(User user) { ??????? ?????????? public String[] SayHiToUserList(List<User> userList) { ? ? 3? 编写 webServiceApp.java类来暴露 web服务。 ??????package com.demo; ????? import javax.xml.ws.Endpoint; ? ????? public class webServiceApp {
?4? run webServiceApp.java 类来启动服务。 访问?http://localhost:8080/helloWorld?wsdl? 查看是否显示 ???? wsdl。 ?? ?5??编写客户端访问服务。 ???? package com.demo; ???? import java.util.ArrayList; ???? import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; ? ???? public class HelloWorldClient { ? ? ? ??6??测试: run webServiceApp.java 类来启动服务,然后 run HelloWorldClient.java 来访问服务。 ? ??二 集成到spring 中。 ?? ? 1 在 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"> ? <welcome-file-list> ? ? <welcome-file>index.jsp</welcome-file> ? </welcome-file-list> ?? ? <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>CXFServlet</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>/webservice/*</url-pattern> ? ? ? ? ? ? ? ?</servlet-mapping> </web-app>
? 2? 在 applicationContext.xml 中加入: ??? <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" ? ? xmlns:jaxws="http://cxf.apache.org/jaxws" ? ? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" ? ? xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" ? ? xsi:schemaLocation="http://www.springframework.org/schema/beans ? ? ? ? ? ?http://www.springframework.org/schema/beans/spring-beans-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/tx ? ? ? ? ? ?http://www.springframework.org/schema/tx/spring-tx-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"> ? ? <!-- 采用注释的方式配置bean --> ? ? <context:annotation-config /> ?? ? ?<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="helloWorld" ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? implementor="com.demo.HelloWorldImpl" ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? address="/helloWorld" /> ? ? ? ? ? ? ? ? ? ? <bean id="client" class="com.demo.HelloWorld"? ? ? ? ? ? ? ? ? ? ? ? ? ? ?factory-bean="clientFactory" factory-method="create"/> ? ?? ? ? ? ? ? ? ? ? ?<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? <property name="serviceClass" value="com.demo.HelloWorld"/> ? ? ? ? ? ? ? ? ? ? ? ? ? ? <property name="address" ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? value="http://localhost:8080/cxf/webservice/helloWorld"/> ? ? ? ? ? ? ? ? ? </bean> ? ? </beans>
? ??? 注意: 这里需要加入 ?xmlns:jaxws="http://cxf.apache.org/jaxws"???和 ??????????????http://cxf.apache.org/jaxws?http://cxf.apache.org/schemas/jaxws.xsd ? ? 3? 修改客户端。 ??????package com.demo; ???? import java.util.ArrayList; ???? import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; ? ???? public class HelloWorldClient { ????????????? } ? ? 4? 发布工程?启动web服务器(我用 tomcat 7)。 ? ??5?访问?http://localhost:8080/你的项目名称/webservice/helloWorld?wsdl??查看是否显示 wsdl 。 ? 6??run run HelloWorldClient.java 来访问服务。(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |