使用CXF 开发SOAP webservice 服务端实例
由于在之前做的一些项目中写数据交换的接口用的都是soap 的webService;所以在昨天晚上特地写了一个基于soap协议 webService的demo实例。在贴出相关demo之前,先对几个名词进行解释: 一个web service 服务的三要素: ? SOAP协议:(Simple Object Access Protocol,即简单对象访问协议)是交换数据的一种协议规范,使用在计算机网络Web服务(web service)中,交换带结构信息;SOAP是基于XML。 WSDL:一个XML格式文档,用以描述服务端口访问方式和使用协议的细节。通常用来辅助生成服务器和客户端代码及配置信息。 UUDI:一个用来发布和搜索WEB服务的协议,应用程序可借由此协议在设计或运行时找到目标WEB服务。 好了,接下来来说说我的那个实例;我是模拟从客户端往服务端同步用户数据的接口服务;是使用Apache CXF进行开发的。首先我要先在Eclipse中创建一个maven工程项目; 然后在pom.xml 引入需要的jar (注意 我是基于spring 集成的 ,使用jetty 进行部署的;所以也要导入对应的jar 和插件)。 然后创建并且配置WSDL,和XSD 文件;然后在pom.xml 中引入apche cxf 支持自动生成WSDL代码的插件: 交互请求、响应实体的soapDemo.xsd 代码:
<?xml version="1.0" encoding="UTF-8"?> <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.deppon.com/soap/soapDemoService" xmlns:tns="http://www.deppon.com/soap/soapDemoService" elementFormDefault="qualified"> <annotation> <documentation>soap webService demo </documentation> </annotation> <element name="demoRequest" type="tns:demoRequest" /> <element name="demoResponse" type="tns:demoResponse" /> <complexType name="demoRequest"> <sequence> <element name="userName" minOccurs="1" maxOccurs="1" type="string"> <annotation> <documentation>用户名</documentation> </annotation> </element> <element name="userCode" minOccurs="1" maxOccurs="1" type="string"> <annotation> <documentation>用户工号</documentation> </annotation> </element> <element name="title" minOccurs="1" maxOccurs="1" type="string"> <annotation> <documentation>职位</documentation> </annotation> </element> </sequence> </complexType> <complexType name="demoResponse"> <sequence> <element name="userName" minOccurs="1" maxOccurs="1" type="string"> <annotation> <documentation>用户名</documentation> </annotation> </element> <element name="resultCode" type="string"> <annotation> <documentation>1 成功,0 系统异常</documentation> </annotation> </element> <element name="resultReason" type="string"> <annotation> <documentation>错误原因</documentation> </annotation> </element> </sequence> </complexType> </schema>交互服务Service的WSDL配置: <?xml version="1.0" encoding="UTF-8" standalone="no"?> <wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://www.deppon.com/soap/soapDemoService" xmlns:tns_domain="http://www.deppon.com/soap/soapDemoService" xmlns:tns_header="http://www.deppon.com/soap/header" xmlns:tns_exception="http://www.deppon.com/soap/exception" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="soapDemoServiceImpl" targetNamespace="http://www.deppon.com/soap/soapDemoService" > <wsdl:types> <xsd:schema targetNamespace="http://www.deppon.com/soap/soapDemoService"> <xsd:include schemaLocation="soapDemo.xsd"></xsd:include> </xsd:schema> <xsd:schema targetNamespace="http://www.deppon.com/soap/header"> <xsd:include schemaLocation="ESBHeader.xsd" /> </xsd:schema> <xsd:schema targetNamespace="http://www.deppon.com/soap/exception"> <xsd:include schemaLocation="CommonException.xsd" /> </xsd:schema> </wsdl:types> <wsdl:message name="ESBHeader"> <wsdl:part name="header" element="tns_header:esbHeader" > </wsdl:part> </wsdl:message> <wsdl:message name="CommonException"> <wsdl:part name="commonExceptionInfo" element="tns_exception:commonExceptionInfo" > </wsdl:part> </wsdl:message> <wsdl:message name="demoRequest"> <wsdl:part name="esbHeader" element="tns_header:esbHeader"></wsdl:part> <wsdl:part name="demoRequest" element="tns_domain:demoRequest"></wsdl:part> </wsdl:message> <wsdl:message name="demoResponse"> <wsdl:part name="esbHeader" element="tns_header:esbHeader"></wsdl:part> <wsdl:part name="demoResponse" element="tns_domain:demoResponse"></wsdl:part> </wsdl:message> <wsdl:portType name="soapDemoService"> <wsdl:operation name="soapDemoService"> <wsdl:input message="tns:demoRequest"/> <wsdl:output message="tns:demoResponse"/> <wsdl:fault name="commonException" message="tns:CommonException"></wsdl:fault> </wsdl:operation> </wsdl:portType> <wsdl:binding name="SoapDemoServiceImplBinding" type="tns:soapDemoService"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> <wsdl:operation name="soapDemoService" xmlns="http://www.deppon.com/soap/soapDemoService"> <!-- <soap:operation soapAction="http://www.deppon.com/ows/waybillStatusBack"/> --> <wsdl:input> <soap:header use="literal" part="esbHeader" message="tns:demoRequest"></soap:header> <soap:body use="literal" parts="demoRequest"/> </wsdl:input> <wsdl:output> <soap:header use="literal" part="esbHeader" message="tns:demoResponse"></soap:header> <soap:body use="literal" parts="demoResponse"/> </wsdl:output> <wsdl:fault name="commonException" xmlns="http://www.deppon.com/soap/exception"> <soap:fault use="literal" name="commonException"/> </wsdl:fault> </wsdl:operation> </wsdl:binding> <wsdl:service name="SoapDemoServiceImpl"> <wsdl:port binding="tns:SoapDemoServiceImplBinding" name="SoapDemoServiceImplPort"> <soap:address location="http://10.224.70.24:8090/test/com/soapDemoService"/> </wsdl:port> </wsdl:service> </wsdl:definitions>?在pom.xml 中配置cxf 自动生成WSDL的插件: <!-- wsdl生成service插件 --> <plugin> <groupId>org.apache.cxf</groupId> <artifactId>cxf-codegen-plugin</artifactId> <version>${cxf-version}</version> <executions> <execution> <id>generate-sources</id> <phase>generate-sources</phase> <configuration> <fork>once</fork> <additionalJvmArgs>-Dfile.encoding=UTF-8</additionalJvmArgs> <!-- <encoding>UTF-8</encoding> --> <defaultOptions> <extraargs> <extraarg>-fe</extraarg> <extraarg>jaxws21</extraarg> </extraargs> </defaultOptions> <!-- <sourceRoot>${project.build.directory}/generated-sources/cxf</sourceRoot> --> <sourceRoot>${basedir}/src/main/java/</sourceRoot> <wsdlOptions> <wsdlOption> <wsdl>${basedir}/src/main/resources/wsdl/com/soap/test/server/soapDemoService.wsdl</wsdl> </wsdlOption> </wsdlOptions> </configuration> <goals> <goal>wsdl2java</goal> </goals> </execution> </executions> </plugin>接下来,通过插件 生成对应的request、response实体类,和Service;并且创建实现服务端接口的实现类UserServiceImpl: UserServiceImpl的代码: <pre name="code" class="java">/** * webService 服务端实现类 * @author leo * */ public class UserServiceImpl implements SoapDemoService{ //???????? private static final Log LOG = LogFactory.getLog(UserServiceImpl.class); /** * soap 接口实现的方法 */ @Override public DemoResponse soapDemoService(Holder<ESBHeader> esbHeader,DemoRequest demoRequest) throws CommonException { DemoResponse res =new DemoResponse(); if(null != demoRequest ){ LOG.info(demoRequest.getUserName()); LOG.info(demoRequest.getTitle()); res.setResultCode("1"); res.setResultReason("login sucess"); } return res; } } 接下来配置web.xml 和配置sping.xml中的接口服务: <?xml version="1.0" encoding="UTF-8"?> <web-app id="web" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name> alert job</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:com/deppon/rest/META-INF/ds-spring.xml</param-value> </context-param> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 日志配置文件的位置 --> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>/WEB-INF/log4j.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- webApp 配置 --> <servlet> <servlet-name>CXFService</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CXFService</servlet-name> <url-pattern>/soap/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app> spring配置文件中配置服务: <bean id="userServiceImpl" class="com.deppon.soap.server.Service.UserServiceImpl"/> <!-- web service--> <jaxws:endpoint id="userServiceImplDemo" implementor="#userServiceImpl" address="/userServiceImpl"> <jaxws:features> <bean class="org.apache.cxf.feature.LoggingFeature"></bean> </jaxws:features> <jaxws:inInterceptors> <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"/> </jaxws:inInterceptors> <jaxws:outInterceptors> <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"/> </jaxws:outInterceptors> 配置好了这些,之后一个SOAP的webService 接口的服务端就写好了。然后启动jetty 服务,测试一下看下服务是否已经通了。 说明服务端已经通了, (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |