介紹
-
遠程調用web服務,不需要自己編寫具體代碼,只需要調用作者給出的接口即可.
-
我們可以調用互聯網上查詢天氣信息Web服務,然后將它嵌入到我們的程序(C/S或B/S程序)當中來,當用戶從我們的網點看到天氣信息時,他會認為我們為他提供了很多的信息服務,但其實我們什么也沒有做,只是簡單調用了一下服務器上的一段代碼而已。
規則
-
基於http 協議
-
傳輸的內容為xml格式
-
SOAP作為一個基於XML語言的協議用於在網上傳輸數據。
- SOAP = 在HTTP的基礎上+XML數據。
-
WSDL – WebService Description Language – Web服務描述語言。
- 用來描述服務的地址和基本方法,java程序通過WSDL來生成對應的代理類來調用具體的方法
Java內置WebService實現
-
服務端(java 項目),不需要引入jar。
// 服務端啟動服務 @WebService // 注解 public class HelloService { public String sayHello(String name){ System.out.println("服務端say hello"); return "hello"+name; } public static void main(String[] args) { // 啟動服務 String address="http://172.27.109.133:8081/hello"; // ip為本機ip 打開cmd 輸入ipconfig 找到ipv4即可 Object implementor=new HelloService(); Endpoint.publish(address, implementor); } }
-
當服務端啟動服務以后,訪問
http://172.27.109.133:8081/hello?wsdl
出現以下內容則表示服務成功啟動,如下圖:
-
-
客戶端
-
首先需要通過wsimport命令解析出對應的Java文件,然后復制到項目目錄
/* * 調用服務 * cmd進入到任意目錄 執行命令 wsimport -s . http://192.168.0.108:8080/hello?wsdl * 復制文件到項目 * 然后進行調用 * */ public class app { public static void main(String[] args) { // 調用方法 HelloServiceService helloServiceService = new HelloServiceService(); HelloService helloServicePort = helloServiceService.getHelloServicePort(); String sayHello = helloServicePort.sayHello("笑笑"); System.out.println(sayHello); } }
-
-
調用成功
使用CXF框架和Spring整合
CXF是apache旗下的開源框架,由Celtix + XFire這兩門經典的框架合成,是一套非常流行的web service框架。
注意:CXF2.X版本和Spring4.X不兼容,要用CXF3.X
服務端
-
編寫接口
@WebService public interface HelloWorldInterface { String sayHello(String text); }
-
編寫實現類
//@WebService在實現類的注解讓CXF知道WSDL創建時所使用的接口。
@WebService(endpointInterface = "webservice.service.HelloWorldInterface")
public class ServerToJava implements HelloWorldInterface {
public String sayHello(String text) {
return "hello" + text;
}
}
```
-
導入項目jar Maven:
<properties> <spring.version>4.2.4.RELEASE</spring.version> <cxf.version>3.2.0</cxf.version> </properties> <dependencies> <!--spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</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.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>3.1.8</version> </dependency> <!--web service 以下都是cxf必備的 --> <!--org.apache.cxf.transport.servlet.CXFServlet --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>3.1.8</version> </dependency> <!--不加這個包會報錯Unable to locate spring NamespaceHandler for XML schema namespace [http://cxf.apache.org/jaxws] --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>3.1.8</version> </dependency> <!--java實現webservice,不部署到tomcat,需要jetty包支持 --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http-jetty</artifactId> <version>3.1.8</version> </dependency> </dependencies>
-
配置web.xml,因為CXF是基於Servlet所以要配置對應的Servlet
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" 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_2_5.xsd"> <display-name>webservice_server</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext-service.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <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>/webservice/*</url-pattern> </servlet-mapping> </web-app>
-
創建Spring配置文件 ,
CXF配置可以放在Spring配置文件中,但不要放在SpringMVC文件中
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <!-- jax-ws endpoint定義 --> <bean id="webserviceServer" class="webservice.ServerToJava"/> <!--發布webservice--> <jaxws:endpoint id="myService" implementor="#webserviceServer" address="/web-publish" /> </beans>
-
訪問時的路徑:
http://localhost:8080/webservice_server/webservice/web-publish?wsdl
規則為:主機+項目+CXFServlet的訪問路徑+address,出現對應的wsdl
頁面即部署成功。
客戶端
-
普通Java程序調用
-
當自己有java的webservice的服務端,即擁有接口的時候,可以直接通過接口和地址進行調用
public class ClientForCXF {
public static MyWebService getInterFace() {
JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();
factoryBean.setServiceClass(MyWebService.class);
factoryBean.setAddress("http://localhost:8080/server/web-publish");
return (MyWebService) factoryBean.create();
}public static void main(String[] args) { MyWebService myWebService = getInterFace(); System.out.println("client: " + myWebService.add(1, 3)); }
}
```
-
而往往服務端可能由別的語言實現,或者服務端並非我們自己實現,我們沒有服務端接口,我們只能獲得暴漏的wsdl,並進行調用,這就需要使用wsdl2java生成該wsdl的java客戶端並調用了
-
首先將從官網下載的文件解壓,並且配置環境變量為
C:\Users\Administrator\Downloads\apache-cxf-3.1.14\bin
即解壓出來文件的bin
目錄 -
在
cmd
中輸入wsdl2java -help
輸出信息即表示配置完成, 注意,這里不能使用jdk的wsimport
-
cmd進入任意文件夾,運行命令
wsdl2java -encoding utf-8 http://localhost:8080/webservice_server/webservice/web-publish?wsdl
然后將生成的文件復制到目錄下,隨后直接調用就可以
-
public static void main(String[] args)
ServerToJavaService javaService = new ServerToJavaService();
HelloWorldInterface c = javaService.getServerToJavaPort();
String a = c.sayHello("s");
System.out.println(a);
}
``` -
-
通過Spring容器
-
需要有CXF和Spring的jar
-
先將接口(只是接口即可)生成出來放到項目中
-
編寫配置文件
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <jaxws:client id="crmClient" serviceClass="webservice_client.HelloWorldInterface" <!-- 對應的接口--> address="http://localhost:8080/webservice_server/webservice/web-publish"/> <!-- 地址 --> </beans>
-
最后和使用平常的bean一樣使用就可以
public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext-service.xml"); HelloWorldInterface c = (HelloWorldInterface) applicationContext.getBean("crmClient"); String str = c.sayHello("終於等到你"); System.out.println(str); }
-
結果
-
類似的框架還有Alibaba的開源項目Dubbo ,這是入門介紹:http://www.cnblogs.com/liyuhui-Z/p/7799615.html