? ? ? ? 由于公司现在是.Net系统于Java系统并存,项目中难免需要跨平台的远程服务调用。最近刚好有一个项目需要Java Web系统调用.Net提供的WebService服务。权衡了下各个因素,最后决定使用 Apache CXF框架,没有用过的同学可以参考http://cxf.apache.org/.
?
????????
废话不多说,上例子,方便大家参考,也给自己做个备份。
项目环境:Spring、Struts2、Ibatis、Maven
一、添加依赖
?
- <dependency>??
- ?????<groupId>org.apache.cxf</groupId>??
- ?????<artifactId>cxf-rt-core</artifactId>??
- ?????<version>2.2.4</version>??
- </dependency>??
- <dependency>??
- ?????<groupId>org.apache.cxf</groupId>??
- ?????<artifactId>cxf-rt-transports-http</artifactId>??
- ?????<version>2.2.4</version>??
- </dependency>??
- <dependency>??
- ?????<groupId>org.apache.cxf</groupId>??
- ?????<artifactId>cxf-rt-databinding-aegis</artifactId>??
- ?????<version>2.2.4</version>??
- </dependency>??
- <dependency>??
- ?????<groupId>org.apache.cxf</groupId>??
- ?????<artifactId>cxf-rt-frontend-jaxws</artifactId>??
- ?????<version>2.2.4</version>??
- </dependency>??
- <dependency>??
- ?????<groupId>commons-httpclient</groupId>??
- ?????<artifactId>commons-httpclient</artifactId>??
- ?????<version>3.1</version>??
- </dependency> ??
二、定义接口
根据wsdl文件,创建对应的Java服务。
?
- <wsdl:definitions?xmlns:s="http://www.w3.org/2001/XMLSchema"?xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/"?xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/"?xmlns:tns="http://tempuri.org/"?xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"?xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/"?xmlns:http="http://schemas.xmlsoap.org/wsdl/http/"?xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"?xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"?targetNamespace="http://tempuri.org/">??
- <wsdl:types>??
- <s:schema?elementFormDefault="qualified"?targetNamespace="http://tempuri.org/">??
- <s:element?name="AddUser">??
- <s:complexType>??
- <s:sequence>??
- <s:element?minOccurs="1"?maxOccurs="1"?name="name"?type="s:string"/>??
- <s:element?minOccurs="1"?maxOccurs="1"?name="password"?type="s:string"/>??
- </s:sequence>??
- </s:complexType>??
- </s:element>??
- <s:element?name="AddUserResponse">??
- <s:complexType>??
- <s:sequence>??
- <s:element?minOccurs="1"?maxOccurs="1"?name="AddUserResult"?type="s:int"/>??
- </s:sequence>??
- </s:complexType>??
- </s:element>??
- </s:schema>??
- </wsdl:types>??
- ...??
- </wsdl:definitions> ?
- package?com.test.ws;??
- import?javax.jws.WebMethod;??
- import?javax.jws.WebParam;??
- import?javax.jws.WebResult;??
- import?javax.jws.WebService;??
- @WebService (?targetNamespace="http://tempuri.org/"?)??
- public?interface?TestService?{??
- ????@WebMethod(operationName="AddUser")??
- ????@WebResult(targetNamespace="http://tempuri.org/",name=AddOrderResult"?)??
- ????public?int?addUser(??
- @WebParam(name="name",targetNamespace="http://tempuri.org/")String?name,???
- @WebParam(name="password",targetNamespace="http://tempuri.org/")String?password);??
- }??
三、配置服务
?
- <bean?id="testService"?class="com.test.ws.TestService"???
- factory-bean="clientFactory"?factory-method="create"/>??
- <bean?id="clientFactory"?class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">??
- <property?name="serviceClass"?value="com.test.ws.TestService"/>??
- <property?name="address"?value="http://192.168.1.101/Service/TestService.asmx"/>??
- <property?name="bindingId"?value="http://schemas.xmlsoap.org/wsdl/soap12/"/>??
- </bean> ?
完成以后,就可以像引用本地服务一样使用该远程服务了。
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|