源码下载地址:http://download.csdn.net/detail/biboheart/6314985
webservice的一个大亮点就是跨平台。项目中要求c++和jee相结合的系统。我想采用webservice来交互信息。
c++做webservice应用,我选择了gsoap;这套系统是B/S模式的应用系统,我选择了ssh三大框架来做。
有一项技术一直没有应用过,经过一天的尝试,终于走通了流程。c++和jee之间实现了相互调用。
在这过程中,我网上查了许多资料,用了很多的关键词搜索,一直没有找到我想要的答案。特别是CXF做客户端的,几乎都是官网文档的部分翻译。都简单的写了一下怎么做,让人摸不着头脑。最后,从一个简单的源码受到启示。尝试成功。
C++的webservice服务端、客户端;jee的服务端、客户端。共有4个项目,为了测试相互调用。所以分了四篇博文。
源码在完成博文后传到csdn资源中供下载。
给自己留个记录,也希望能帮到跟我一样迷茫中的初学者。
首先是WSDL,4个项目使用同一个WSDL。我是初学者,不会编辑WSDL文档,所以就先用CXF代码优先做个服务器端,成功后产生WSDL,再给其它3个项目使用。
我之前的一篇博文中有使用描述(struts2+spring3+hibernate4+cxf2.7搭建webservice实例)
在这里不说引入包之类的了。就写开发过程。
建立Dynamic Web Project
project name:CXFServerDemo
开始配置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" metadata-complete="true" version="3.0"> <display-name>webservicetest</display-name> <description>webservicetest</description> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- CXF --> <servlet> <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>/webServices/*</url-pattern> </servlet-mapping> <session-config> <session-timeout>60</session-timeout> </session-config> <!-- struts2 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- spring --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:applicationContext-*.xml</param-value> </context-param> <!-- log4j --> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:log4j.properties</param-value> </context-param> <context-param> <param-name>log4jRefreshInterval</param-name> <param-value>60000</param-value> </context-param> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> </web-app>
建立SSH基本项目的框架,SSH项目结构不是这里要描述的。完成后如下图
在图中还没有开始webservice的情况。
建立org.biboheart.webservice包,类名:HelloWorld
建立接口后实现
HelloWorld.java中的内容:
package org.biboheart.webservice; import javax.jws.WebParam; import javax.jws.WebService; @WebService public interface HelloWorld { public String sayHello(@WebParam(name = "name") String name); }
package org.biboheart.webservice; import javax.jws.WebParam; import javax.jws.WebService; @WebService(endpointInterface = "org.biboheart.webservice.HelloWorld",serviceName = "HelloWorld") public class HelloWorldImpl implements HelloWorld { @Override public String sayHello(@WebParam(name = "name") String name) { String hello = "hello " + name; System.out.println(hello); return hello; } }
配置spring:只写了片段,后面的hibernate、其它的bean的装配太多,不全放出了
<?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:context="http://www.springframework.org/schema/context" xmlns:jaxws="http://cxf.apache.org/jaxws" 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/context http://www.springframework.org/schema/context/spring-context-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://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <context:annotation-config /> <context:component-scan base-package="org.biboheart" /> <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="webServiceHelloWorld" address="/HelloWorld" implementorClass="org.biboheart.webservice.HelloWorldImpl"/>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="vcs-base" extends="struts-default"> <!-- 配置全局结果 --> <global-results> <result name="index">/index.jsp</result> </global-results> </package> <package name="webServices" extends="vcs-base" namespace="/webServices"> <action name="*"> <result>{1}</result> </action> </package> </struts>
将项目加入tomcat中启动项目。
在浏览器中输入:http://localhost:8080/CXFServerDemo/webServices/HelloWorld?wsdl
<?xml version="1.0" encoding='UTF-8'?><wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://webservice.biboheart.org/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="HelloWorld" targetNamespace="http://webservice.biboheart.org/"> <wsdl:types> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://webservice.biboheart.org/" elementFormDefault="unqualified" targetNamespace="http://webservice.biboheart.org/" version="1.0"> <xs:element name="sayHello" type="tns:sayHello"></xs:element> <xs:element name="sayHelloResponse" type="tns:sayHelloResponse"></xs:element> <xs:complexType name="sayHello"> <xs:sequence> <xs:element minOccurs="0" name="name" type="xs:string"></xs:element> </xs:sequence> </xs:complexType> <xs:complexType name="sayHelloResponse"> <xs:sequence> <xs:element minOccurs="0" name="return" type="xs:string"></xs:element> </xs:sequence> </xs:complexType> </xs:schema> </wsdl:types> <wsdl:message name="sayHelloResponse"> <wsdl:part element="tns:sayHelloResponse" name="parameters"> </wsdl:part> </wsdl:message> <wsdl:message name="sayHello"> <wsdl:part element="tns:sayHello" name="parameters"> </wsdl:part> </wsdl:message> <wsdl:portType name="HelloWorld"> <wsdl:operation name="sayHello"> <wsdl:input message="tns:sayHello" name="sayHello"> </wsdl:input> <wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse"> </wsdl:output> </wsdl:operation> </wsdl:portType> <wsdl:binding name="HelloWorldSoapBinding" type="tns:HelloWorld"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"></soap:binding> <wsdl:operation name="sayHello"> <soap:operation soapAction="" style="document"></soap:operation> <wsdl:input name="sayHello"> <soap:body use="literal"></soap:body> </wsdl:input> <wsdl:output name="sayHelloResponse"> <soap:body use="literal"></soap:body> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="HelloWorld"> <wsdl:port binding="tns:HelloWorldSoapBinding" name="HelloWorldImplPort"> <soap:address location="http://localhost:8080/CXFServerDemo/webServices/HelloWorld"></soap:address> </wsdl:port> </wsdl:service> </wsdl:definitions>
浏览器中选择保存,把资源保存为.wsdl文件。也可以直接复制上面的代码保存为.wsdl文件进行开发使用。
CXF 服务已经发布完成,下面的用这个WSDL文件在C++方用GSOAP做webservice服务。
该上班了…… 晚上再续