WebService(CXF對外發布WebService服務)(1)
阿新 • • 發佈:2019-01-03
一、建立Maven工程
注意pom.xml中的dependency配置,需要引入這三個包:(特別注意,引入的這三個包的版本要一致,否則會報奇怪的錯誤)
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.rvho</groupId> <artifactId>cxfstandalone</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <!-- 檔案拷貝編碼 --> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!-- 輸出編碼 --> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <!-- 編譯編碼 --> <maven.compiler.encoding>UTF-8</maven.compiler.encoding> <!-- CXF版本 --> <cxf.version>3.0.0</cxf.version> </properties> <dependencies> <!-- CXF --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <!-- 如果CXF不整合到Web伺服器中,必須新增該引用 --> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http-jetty</artifactId> <version>${cxf.version}</version> </dependency> <!-- End CXF --> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.3</version> <configuration> <!-- 指定source和target的jdk版本是1.8 --> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
二、服務端程式碼
1、寫一個對外發布的介面
package com.cah.ddi3.ws; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService; @WebService public interface WebServiceI { @WebMethod String sayHello(@WebParam(name="name") String name); @WebMethod String saveInfo(@WebParam(name="name") String name,@WebParam(name="info") String info); }
2、實現這個介面
package com.cah.ddi3.ws; import javax.jws.WebParam; import javax.jws.WebService; @WebService public class WebServiceImpl implements WebServiceI { @Override public String sayHello(@WebParam(name="name") String name) { // TODO Auto-generated method stub System.out.println("WebService sayHello "+name); return "sayHello "+name; } @Override public String saveInfo(@WebParam(name="name") String name,@WebParam(name="info") String info) { // TODO Auto-generated method stub System.out.println(name+"呼叫WebService介面,save:"+info); return "save Success"; } }
3、釋出這個服務
package com.cah.ddi3.ws;
import javax.xml.ws.Endpoint;
import org.apache.cxf.frontend.ServerFactoryBean;
public class WebServicePublish {
public static void main(String[] args) {
String address = "http://localhost:8080/WS_Server/Webservice";
Endpoint.publish(address , new WebServiceImpl());
System.out.println("釋出webservice成功!");
// ServerFactoryBean sf=new ServerFactoryBean();
// //服務實現類
// sf.setServiceClass(WebServiceImpl.class);
// //服務的釋出地址
// sf.setAddress("http://localhost:8080/WS_Server/Webservice");
// //服務的例項
// sf.setServiceBean(new WebServiceImpl());
// //釋出服務
// sf.create();
// System.out.println("server ready……");
}
}
4、驗證
執行上面的類,在瀏覽器中鍵入:http://localhost:8080/WS_Server/Webservice?wsdl,如果打開了如下的wsdl文件,說明發布成功。
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://ws.ddi3.cah.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="WebServiceImplService" targetNamespace="http://ws.ddi3.cah.com/">
<wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://ws.ddi3.cah.com/" elementFormDefault="unqualified" targetNamespace="http://ws.ddi3.cah.com/" version="1.0">
<xs:element name="saveInfo" type="tns:saveInfo"/>
<xs:element name="saveInfoResponse" type="tns:saveInfoResponse"/>
<xs:element name="sayHello" type="tns:sayHello"/>
<xs:element name="sayHelloResponse" type="tns:sayHelloResponse"/>
<xs:complexType name="sayHello">
<xs:sequence>
<xs:element minOccurs="0" name="name" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="sayHelloResponse">
<xs:sequence>
<xs:element minOccurs="0" name="return" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="saveInfo">
<xs:sequence>
<xs:element minOccurs="0" name="name" type="xs:string"/>
<xs:element minOccurs="0" name="info" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="saveInfoResponse">
<xs:sequence>
<xs:element minOccurs="0" name="return" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
<wsdl:message name="sayHello">
<wsdl:part element="tns:sayHello" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:message name="sayHelloResponse">
<wsdl:part element="tns:sayHelloResponse" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:message name="saveInfo">
<wsdl:part element="tns:saveInfo" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:message name="saveInfoResponse">
<wsdl:part element="tns:saveInfoResponse" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:portType name="WebServiceI">
<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:operation name="saveInfo">
<wsdl:input message="tns:saveInfo" name="saveInfo"></wsdl:input>
<wsdl:output message="tns:saveInfoResponse" name="saveInfoResponse"></wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="WebServiceImplServiceSoapBinding" type="tns:WebServiceI">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="sayHello">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="sayHello">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="sayHelloResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="saveInfo">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="saveInfo">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="saveInfoResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="WebServiceImplService">
<wsdl:port binding="tns:WebServiceImplServiceSoapBinding" name="WebServiceImplPort">
<soap:address location="http://localhost:8080/WS_Server/Webservice"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
三、客戶端程式碼
建立一個WebService客戶端測試專案,藉助jdk的wsimort.exe工具生成客戶端程式碼,wsimort.exe工具位於Jdk的bin目錄下,開啟命令列視窗,切換到src目錄,執行"wsimport -keep http://localhost:8080/WS_Server/Webservice?wsdl"生成客戶端程式碼,如下圖所示:
在客戶端工程的src目錄下可以生成如下程式碼:
建立WsClient.java類,程式碼如下:
package com.cah.ddi3.ws;
public class WsClient {
public static void main(String[] args) {
// TODO Auto-generated method stub
WebServiceImplService factory = new WebServiceImplService();
WebServiceI wsImpl = factory.getWebServiceImplPort();
String resResult = wsImpl.sayHello("小魚兒");
System.out.println("呼叫WebService的sayHello方法返回的結果是:"+resResult);
System.out.println("---------------------------------------------------");
resResult = wsImpl.saveInfo("小魚兒", "歡迎你!");
System.out.println("呼叫WebService的saveInfo方法返回的結果是:"+resResult);
}
}
執行上述類,控制檯打出:
呼叫成功!