CXF 簡單創建Webserver 例子
最近在弄webserver,因為公司需要用到,來說說,webserver的常用方式吧
1.什麽是webservice
1.1 什麽是遠程調用技術
遠程調用數據定義:是系統和系統之間的調用
先說一說常用的webserver 的客戶端方式吧
Webservice的四種客戶端調用方式
公網服務地址:
http://www.webxml.com.cn/zh_cn/index.aspx
1.1 第一種生成客戶端調用方式
1.1.1 Wsimport命令介紹
l Wsimport就是jdk提供的的一個工具,他作用就是根據WSDL地址生成客戶端代碼
l Wsimport位置JAVA_HOME/bin
l Wsimport常用的參數:
- -s,生成java文件的
- -d,生成class文件的,默認的參數
- -p,指定包名的,如果不加該參數,默認包名就是wsdl文檔中的命名空間的倒序
l Wsimport僅支持SOAP1.1客戶端的生成
1.1.2 調用公網手機號歸屬地查詢服務
l 第一步:wsimport生成客戶端代碼
wsimport -p cn.itcast.mobile -s . http://webservice.we bxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl |
l 第二步:閱讀使用說明書,使用生成客戶端代碼調用服務端
package cn.itcast.mobile.client;
import cn.itcast.mobile.MobileCodeWS; import cn.itcast.mobile.MobileCodeWSSoap;
/** * * <p>Title: MobileClient.java</p> * <p>Description:公網手機號查詢客戶端</p> * <p>Company: www.itcast.com</p> * @author at * @date 2015年11月26日下午3:16:05 * @version 1.0 */ public class MobileClient {
public static void main(String[] args) { //創建服務視圖 MobileCodeWS mobileCodeWS = new MobileCodeWS(); //獲取服務實現類 MobileCodeWSSoap mobileCodeWSSoap = mobileCodeWS.getPort(MobileCodeWSSoap.class); //調用查詢方法 String reuslt = mobileCodeWSSoap.getMobileCodeInfo("13888888", null); System.out.println(reuslt); } }
|
1.1.3 公網天氣服務端查詢
package cn.itcast.mobile.client;
import java.util.List;
import cn.itcast.weather.ArrayOfString; import cn.itcast.weather.WeatherWS; import cn.itcast.weather.WeatherWSSoap;
/** * * <p>Title: WeatherClient.java</p> * <p>Description:公網天氣查詢客戶端</p> * <p>Company: www.itcast.com</p> * @author at * @date 2015年11月26日下午3:24:12 * @version 1.0 */ public class WeatherClient {
public static void main(String[] args) { WeatherWS weatherWS = new WeatherWS(); WeatherWSSoap weatherWSSoap = weatherWS.getPort(WeatherWSSoap.class); ArrayOfString arrayOfString = weatherWSSoap.getWeather("北京", ""); List<String> list = arrayOfString.getString();
for(String str : list){ System.out.println(str); } } }
|
1.1.4 特點
該種方式使用簡單,但一些關鍵的元素在代碼生成時寫死到生成代碼中,不方便維護,所以僅用於測試。
2 第二種:service編程調用方式
package cn.itcast.mobile.client;
import java.io.IOException; import java.net.MalformedURLException; import java.net.URL;
import javax.xml.namespace.QName; import javax.xml.ws.Service;
import cn.itcast.mobile.MobileCodeWSSoap;
/** * * <p>Title: ServiceClient.java</p> * <p>Description:Service編程實現服務端調用</p> * <p>Company: www.itcast.com</p> * @author at * @date 2015年11月26日下午3:43:55 * @version 1.0 */ public class ServiceClient {
public static void main(String[] args) throws IOException { //創建WSDL的URL,註意不是服務地址 URL url = new URL("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl");
//創建服務名稱 //1.namespaceURI - 命名空間地址 //2.localPart - 服務視圖名 QName qname = new QName("http://WebXml.com.cn/", "MobileCodeWS");
//創建服務視圖 //參數解釋: //1.wsdlDocumentLocation - wsdl地址 //2.serviceName - 服務名稱 Service service = Service.create(url, qname); //獲取服務實現類 MobileCodeWSSoap mobileCodeWSSoap = service.getPort(MobileCodeWSSoap.class); //調用查詢方法 String result = mobileCodeWSSoap.getMobileCodeInfo("1866666666", ""); System.out.println(result); } }
|
2.1 特點
該種方式可以自定義關鍵元素,方便以後維護,是一種標準的開發方式
3 第三種:HttpURLConnection調用方式
開發步驟:
第一步:創建服務地址
第二步:打開一個通向服務地址的連接
第三步:設置參數
設置POST,POST必須大寫,如果不大寫,報如下異常
如果不設置輸入輸出,會報如下異常
第四步:組織SOAP數據,發送請求
第五步:接收服務端響應,打印
package cn.itcast.mobile.client;
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL;
/** * * <p>Title: HttpClient.java</p> * <p>Description:HttpURLConnection調用方式</p> * <p>Company: www.itcast.com</p> * @author at * @date 2015年11月26日下午3:58:57 * @version 1.0 */ public class HttpClient {
public static void main(String[] args) throws IOException { //第一步:創建服務地址,不是WSDL地址 URL url = new URL("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx"); //第二步:打開一個通向服務地址的連接 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); //第三步:設置參數 //3.1發送方式設置:POST必須大寫 connection.setRequestMethod("POST"); //3.2設置數據格式:content-type connection.setRequestProperty("content-type", "text/xml;charset=utf-8"); //3.3設置輸入輸出,因為默認新創建的connection沒有讀寫權限, connection.setDoInput(true); connection.setDoOutput(true);
//第四步:組織SOAP數據,發送請求 String soapXML = getXML("15226466316"); OutputStream os = connection.getOutputStream(); os.write(soapXML.getBytes()); //第五步:接收服務端響應,打印 int responseCode = connection.getResponseCode(); if(200 == responseCode){//表示服務端響應成功 InputStream is = connection.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr);
StringBuilder sb = new StringBuilder(); String temp = null; while(null != (temp = br.readLine())){ sb.append(temp); } System.out.println(sb.toString());
is.close(); isr.close(); br.close(); }
os.close(); }
/** * <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <getMobileCodeInfo xmlns="http://WebXml.com.cn/"> <mobileCode>string</mobileCode> <userID>string</userID> </getMobileCodeInfo> </soap:Body> </soap:Envelope> * @param phoneNum * @return */ public static String getXML(String phoneNum){ String soapXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" +"<soap:Body>" +"<getMobileCodeInfo xmlns=\"http://WebXml.com.cn/\">" +"<mobileCode>"+phoneNum+"</mobileCode>" +"<userID></userID>" +"</getMobileCodeInfo>" +"</soap:Body>" +"</soap:Envelope>"; return soapXML; } }
|
4 Ajax調用方式
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script type="text/javascript"> function queryMobile(){ //創建XMLHttpRequest對象 var xhr = new XMLHttpRequest(); //打開連接 xhr.open("post","http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx",true); //設置數據類型 xhr.setRequestHeader("content-type","text/xml;charset=utf-8"); //設置回調函數 xhr.onreadystatechange=function(){ //判斷是否發送成功和判斷服務端是否響應成功 if(4 == xhr.readyState && 200 == xhr.status){ alert(xhr.responseText); } } //組織SOAP協議數據 var soapXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" +"<soap:Body>" +"<getMobileCodeInfo xmlns=\"http://WebXml.com.cn/\">" +"<mobileCode>"+document.getElementById("phoneNum").value+"</mobileCode>" +"<userID></userID>" +"</getMobileCodeInfo>" +"</soap:Body>" +"</soap:Envelope>"; alert(soapXML); //發送數據 xhr.send(soapXML); } </script> </head> <body> 手機號查詢:<input type="text" id="phoneNum"/> <input type="button" value="查詢" onclick="javascript:queryMobile();"/> </body> </html> |
當然是復制的啦,給我自己回憶看的,有興趣一起來更深入的探討啊!
cxf
1 CXF介紹、安裝和配置
1.1 CXF介紹
l CXF是一個開源的webservice框架,提供很多完善功能,可以實現快速開發
l CXF支持的協議:SOAP1.1/1.2,REST
l CXF支持數據格式:XML,JSON(僅在REST方式下支持)
1.2 CXF的安裝和配置
l 下載地址
http://cxf.apache.org/download.html
l 包結構介紹
l 安裝和配置
- 第一步:安裝JDK,建議1.7
- 第二步:解壓apache-cxf-2.7.11.zip到指定目錄,創建CXF_HOME
- 第三步:把CXF_HOME加入到Path路徑下
- 第四步:測試,在cmd下加入wsdl2java –h
- 如果不想使用IDE(比如Eclipse),需要在環境變量下配置如下信息
2 CXF發布SOAP協議的服務
2.1 需求
服務端:發布服務,接收客戶端的城市名,返回天氣數據給客戶端
客戶端:發送城市名給服務端,接收服務端的響應信息,打印
2.2 實現
2.2.1 服務端
開發步驟:
第一步:導入Jar包
第二步:創建SEI接口,要加入@WebService
package cn.itcast.ws.cxf.server;
import javax.jws.WebService;
/** * * <p>Title: WeatherInterface.java</p> * <p>Description:SEI接口</p> * <p>Company: www.itcast.com</p> * @authorat * @date 2015年11月27日上午9:47:43 * @version 1.0 */ @WebService public interface WeatherInterface {
public String queryWeather(String cityName); }
|
第三步:創建SEI實現類
package cn.itcast.ws.cxf.server;
/** * * <p>Title: WeatherInterfaceImpl.java</p> * <p>Description:SEI實現類</p> * <p>Company: www.itcast.com</p> * @authorat * @date 2015年11月27日上午9:50:59 * @version 1.0 */ public class WeatherInterfaceImpl implements WeatherInterface {
@Override public String queryWeather(String cityName) { System.out.println("from client..."+cityName); if("北京".equals(cityName)){ return "冷且霾"; } else { return "暖且晴"; } }
}
|
第四步:發布服務, JaxWsServerFactoryBean發布,設置3個參數,1.服務接口;2.服務實現類;3.服務地址;
endpoint僅支持發布實現類,JaxWsServerFactoryBean支持發布接口。
package cn.itcast.ws.cxf.server;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean; import org.apache.cxf.tools.java2wsdl.processor.internal.jaxws.generator.JaxwsServerGenerator;
/** * * <p>Title: WeatherServer.java</p> * <p>Description:服務端</p> * <p>Company: www.itcast.com</p> * @authorat * @date 2015年11月27日上午9:51:36 * @version 1.0 */ public class WeatherServer {
public static void main(String[] args) { //JaxWsServerFactoryBean發布服務 JaxWsServerFactoryBean jaxWsServerFactoryBean = new JaxWsServerFactoryBean(); //設置服務接口 jaxWsServerFactoryBean.setServiceClass(WeatherInterface.class); //設置服務實現類 jaxWsServerFactoryBean.setServiceBean(new WeatherInterfaceImpl()); //設置服務地址 jaxWsServerFactoryBean.setAddress("http://127.0.0.1:12345/weather"); //發布 jaxWsServerFactoryBean.create(); } }
|
第五步:測試服務是否發布成功,閱讀使用說明書,確定關鍵點
如果在CXF發布的服務下,直接訪問服務地址,會如下異常
此時直接訪問使用說明書地址即可
2.2.1.1 發布SOAP1.2的服務端
l 在接口上加入如下註解:
@BindingType(SOAPBinding.SOAP12HTTP_BINDING)
l 重新發布服務端
2.2.1.2 攔截器
l 原理:
- 攔截器可以攔截請求和響應
- 攔截器可以有多個
- 攔截器可以根據需要自定義
l 使用
- 攔截器必須加到服務端,在服務端發布之前
- 獲取攔截器列表,將自己的攔截器加入列表中
2.2.2 客戶端
第一步:生成客戶端代碼
l Wsdl2java命令是CXF提供的生成客戶端的工具,他和wsimport類似,可以根據WSDL生成客戶端代碼
l Wsdl2java常用參數:
-d,指定輸出目錄
-p,指定包名,如果不指定該參數,默認包名是WSDL的命名空間的倒序
l Wsdl2java支持SOAP1.1和SOAP1.2
第二步:使用說明書,使用生成代碼調用服務端
JaxWsProxyFactoryBean調用服務端,設置2個參數,1.設置服務接口;2.設置服務地址
package cn.itcast.cxf.client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import cn.itcast.cxf.weather.WeatherInterface;
/** * * <p>Title: WeatherClient.java</p> * <p>Description:客戶端</p> * <p>Company: www.itcast.com</p> * @authorat * @date 2015年11月27日上午10:12:24 * @version 1.0 */ public class WeatherClient {
public static void main(String[] args) { //JaxWsProxyFactoryBean調用服務端 JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean(); //設置服務接口 jaxWsProxyFactoryBean.setServiceClass(WeatherInterface.class); //設置服務地址 jaxWsProxyFactoryBean.setAddress("http://127.0.0.1:12345/weather"); //獲取服務接口實例 WeatherInterface weatherInterface = jaxWsProxyFactoryBean.create(WeatherInterface.class); //調用查詢方法 String weather = weatherInterface.queryWeather("保定"); System.out.println(weather); } }
|
接下來是整合spring
1 CXF+Spring整合發布SOAP協議的服務
1.1 服務端
開發步驟:
第一步:創建web項目(引入jar包)
第二步:創建SEI接口
第三步:創建SEI實現類
第四步:配置spring配置文件,applicationContext.xml,用<jaxws:server標簽發布服務,設置1.服務地址;2.設置服務接口;3設置服務實現類
<?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:jaxws="http://cxf.apache.org/jaxws" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd"> <!-- <jaxws:server發布SOAP協議的服務 ,對JaxWsServerFactoryBean類封裝--> <jaxws:server address="/weather" serviceClass="cn.itcast.ws.cxf.server.WeatherInterface"> <jaxws:serviceBean> <ref bean="weatherInterface"/> </jaxws:serviceBean> </jaxws:server> <!-- 配置服務實現類 --> <bean name="weatherInterface" class="cn.itcast.ws.cxf.server.WeatherInterfaceImpl"/> </beans> |
第五步:配置web.xml,配置spring配置文件地址和加載的listener,配置CXF的servlet
<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>ws_2_cxf_spring_server</display-name>
<!-- 設置spring的環境 --> <context-param> <!--contextConfigLocation是不能修改的 --> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
<!-- 配置CXF的Servlet --> <servlet> <servlet-name>CXF</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CXF</servlet-name> <url-pattern>/ws/*</url-pattern> </servlet-mapping>
<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> </web-app> |
第六步:部署到tomcat下,啟動tomcat
第七步:測試服務,閱讀使用說明書
WSDL地址規則:http://ip:端口號/項目名稱/servlet攔截路徑/服務名稱?wsdl
1.1.1 攔截器配置
配置applicationContext.xml中。
1.1.2 Endpoint標簽發布服務
<jaxws:endpoint>標簽
package cn.itcast.ws.cxf.server;
import javax.jws.WebService;
/** * * <p>Title: HelloWorld.java</p> * <p>Description:簡單類</p> * <p>Company: www.itcast.com</p> * @author at * @date 2015年11月27日上午11:11:10 * @version 1.0 */ @WebService public class HelloWorld { public String sayHello(String name){ return "hello,"+name; }
}
|
1.2 客戶端
開發步驟:
第一步:引入jar包
第二步:生成客戶端代碼
第三步:配置spring配置文件,applicationContent.xml
<?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:jaxws="http://cxf.apache.org/jaxws" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd"> <!-- <jaxws:client實現客戶端 ,對JaxWsProxyFactoryBean類封裝--> <jaxws:client id="weatherClient" address="http://127.0.0.1:8080/ws_2_cxf_spring_server/ws/weather" serviceClass="cn.itcast.cxf.weather.WeatherInterface"/> </beans> |
第四步:從spring上下文件獲取服務實現類
第五步:調用查詢方法,打印
package cn.itcast.cxf.client;
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.itcast.cxf.weather.WeatherInterface;
public class WeatherClient {
public static void main(String[] args) { //初始化spring的上下文 ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); WeatherInterface weatherInterface = (WeatherInterface) context.getBean("weatherClient"); String weather = weatherInterface.queryWeather("保定"); System.out.println(weather); } }
|
就先這樣吧0.0
CXF 簡單創建Webserver 例子