CXF搭建webService服务器(maven or jar)
? ?欢迎来到:http://observer.blog.51cto.com 一:webService背景 ? ?说到webService,就涉及到XML还有wsdl。 <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring.version>3.1.1.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> </dependency> <!-- spring starts --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.6.10</version> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib-nodep</artifactId> <version>2.1_3</version> </dependency> <!-- spring ends --> <!-- cxf starts --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-bundle</artifactId> <version>2.5.0</version> <!-- 此包在整合spring跟hibernate时出现包冲突,在此去掉 --> <exclusions> <exclusion> <groupId>asm</groupId> <artifactId>asm</artifactId> </exclusion> </exclusions> </dependency> <!-- cxf ends --> </dependencies> ? ?在此也许会有人会问,为什么还要加上spring的包?一,cxf框架依赖于spring。二,maven会自动导进依赖,但是在给cxf导进spring依赖时,我这里却只导进了一些依赖标准文件,实际没有导进jar包,所以必须加上去,也许有些朋友的maven版本或者导进的cxf包版本不一样,产生的效果会不一样也说不定。 第三步:配置web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" 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_3_0.xsd" metadata-complete="true"> <display-name>myserviceCXF</display-name> <!-- cxf全局变量 --> <context-param> <param-name>webAppRootKey</param-name> <param-value>limg.cxf.example</param-value> </context-param> <!-- cxf配置文件位置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:/cxf.xml</param-value> </context-param> <!-- cxf servlet starts --> <servlet> <description>Apache CXF Endpoint</description> <display-name>cxf</display-name> <servlet-name>cxf</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>cxf</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> <!-- cxf servlet end --> <session-config> <session-timeout>60</session-timeout> </session-config> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app> 第四步:定义接口 package com.observer.cxf.service; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; @WebService(targetNamespace="http://www.observer.com/service",serviceName="GradeService") public interface GradeService { @WebMethod(operationName="getGradeName") @WebResult(name = "result") public String getGradeName(@WebParam(name = "toid")long toid); } ? ?以上配置中,加上去的标签是用于webService生成wsdl用的,定义方法名,service名等,这里值得注意的是targetNamespace的值,要是没有他,那么系统会默认为你的包名,如果是这样,那么你对外开放方法之后,在wsdl中和客户端中都会暴露你的服务器的包路径,这个就危险了,所以强烈建议改成自己所希望显示的。在此,各位朋友大可以自己动手试试。 package com.observer.cxf.service; import javax.jws.WebService; @WebService(endpointInterface = "com.observer.cxf.service.GradeService") public class GradeServiceImpl implements GradeService { public String gradeName(long toid) { return "is succeed"; } } ? ?以上配置endpointInterface中的字符串为自己定义的webservice接口。 <?xml version="1.0" encoding="UTF-8"?> <!-- START SNIPPET: beans --> <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 name="gradeService" class="com.observer.cxf.service.GradeServiceImpl"/> <jaxws:server id="services" serviceClass="com.observer.cxf.service.GradeService" address="/Service"> <jaxws:serviceBean> <ref bean="gradeService"/> </jaxws:serviceBean> </jaxws:server> <!-- <jaxws:server id="services" serviceClass="com.observer.cxf.service.GradeService" address="/Service"> <jaxws:serviceBean> <bean class="com.observer.cxf.service.GradeServiceImpl"/> </jaxws:serviceBean> </jaxws:server> --> </beans> ? ?以上配置,一种为另外定义一个bean,然后放进去,一种是直接在server中放进去,个人所需,自己选择吧!在此,如果另外定义了bean,比如说:使用spring的注释定义了一个class bean,那么就可以直接使用第一种方式加进去就可以了。 ? ?然后点击:{http://service.cxf.observer.com/}GradeService;出现webService的wsdl: ? ?出现以上现象,恭喜你,你的CXF框架webService服务器已经搭建成功。 ? ?我的源码可以到此下载:webServiceCXFmaven.rar 三:下载jar包导进去搭建 ? ?新建web项目:webServiceCXF (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |