java實現WebService 以及客戶端不同的調用方式


 

java 實現WebService 以及不同的調用方式

webservice:
    就是應用程序之間跨語言的調用
    wwww.webxml.com.cn
    1.xml
    2.    wsdl: webservice description language web服務描述語言
        通過xml格式說明調用的地址方法如何調用,可以看錯webservice的說明書
    
    3.soap simple object access protoacl (簡單對象訪問協議)
        限定了xml的格式
        soap 在http(因為有請求體,所以必須是post請求)的基礎上傳輸xml數據
            請求和響應的xml 的格式如:    <Envelop>
                                <body>
                                //....
                                </body>
                            </Envelop>
                operation name:服務提供的方法
                
        
    靜態方法不能發布為外部服務
    
    運用jkd自帶的代碼生成訪問服務器的客戶端代碼    E:/wsimort -s . http://test.cm/?wsdl
    
    我們可以把webservice看做是web服務器上的一個應用,web服務器是webservice的一個容器
    
    函數的參數在 http://test.cm/?xsd=1
    
    JAX-WS是指 java api for xml -WebService
    
    //測試 WebService服務的 explorer
    Web Service Explorer 可以顯示返回的xml格式
    
    targetNamespace 默認為倒置的包名
    
客戶端調用WebService的方式:
    1.通過wximport生成代碼
    2.通過客戶端編程方式
    3.通過ajax調用方式
    4.通過 URL Connection 方式調用


請求過程分析:
        1.使用get方式獲取wsdl文件,稱為握手
        2.使用post發出請求
        3.服務器響應成功過
    


幾種監聽工具:
    http watch
    Web Service explorer
    eclipse 自帶工具   TCP/IP Monitor
   

 

 

   
    服務端代碼:

復制代碼
package com.webservcie; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; import javax.xml.ws.Endpoint; /** * WebService * 將 Java 類標記為實現 Web Service,或者將 Java 接口標記為定義 Web Service 接口 */ @WebService(serviceName="MyService",targetNamespace="http://www.baidu.com") public class HelloService { @WebMethod(operationName="AliassayHello") @WebResult(name="myReturn") public String sayHello(@WebParam(name="name") String name){ return "hello: " + name; } public String sayGoodbye(String name){ return "goodbye: " + name; } @WebMethod(exclude=true)//當前方法不被發布出去 public String sayHello2(String name){ return "hello " + name; } public static void main(String[] args) { /** * 參數1:服務的發布地址 * 參數2:服務的實現者 * Endpoint 會重新啟動一個線程 */ Endpoint.publish("http://test.cm/", new HelloService()); System.out.println("Server ready..."); } }
復制代碼

 

1.客戶端調用(wximport自動生成代碼 【推薦】)

復制代碼
package com.wsclient; public class App { /** * 通過wsimport 解析wsdl生成客戶端代碼調用WebService服務 * * @param args * */ public static void main(String[] args) { // TODO Auto-generated method stub /** * <service name="MyService"> * 獲得服務名稱 */ MyService mywebService = new MyService(); /** * <port name="HelloServicePort" binding="tns:HelloServicePortBinding"> */ HelloService hs = mywebService.getHelloServicePort(); /** * 調用方法 */ System.out.println(hs.sayGoodbye("sjk")); System.out.println(hs.aliassayHello("sjk")); } }
復制代碼

 2.通過ajax+js+xml調用

復制代碼
<html> <head> <title>通過ajax調用WebService服務</title> <script> var xhr = new ActiveXObject("Microsoft.XMLHTTP"); function sendMsg(){ var name = document.getElementById('name').value; //服務的地址 var wsUrl = 'http://192.168.1.100:6789/hello'; //請求體 var soap = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://ws.itcast.cn/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">' + ' <soapenv:Body> <q0:sayHello><arg0>'+name+'</arg0> </q0:sayHello> </soapenv:Body> </soapenv:Envelope>'; //打開連接  xhr.open('POST',wsUrl,true); //重新設置請求頭  xhr.setRequestHeader("Content-Type","text/xml;charset=UTF-8"); //設置回調函數  xhr.onreadystatechange = _back; //發送請求  xhr.send(soap); } function _back(){ if(xhr.readyState == 4){ if(xhr.status == 200){ //alert('調用Webservice成功了'); var ret = xhr.responseXML; var msg = ret.getElementsByTagName('return')[0]; document.getElementById('showInfo').innerHTML = msg.text; //alert(msg.text);  } } } </script> </head> <body> <input type="button" value="發送SOAP請求" onclick="sendMsg();"> <input type="text" id="name"> <div id="showInfo"> </div> </body> </html>
復制代碼

 

3.URL Connection方式

復制代碼
import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; /** * 通過UrlConnection調用Webservice服務 * */ public class App { public static void main(String[] args) throws Exception { //服務的地址 URL wsUrl = new URL("http://192.168.1.100:6789/hello"); HttpURLConnection conn = (HttpURLConnection) wsUrl.openConnection(); conn.setDoInput(true); conn.setDoOutput(true); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8"); OutputStream os = conn.getOutputStream(); //請求體 String soap = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:q0=\"http://ws.itcast.cn/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" + "<soapenv:Body> <q0:sayHello><arg0>aaa</arg0> </q0:sayHello> </soapenv:Body> </soapenv:Envelope>"; os.write(soap.getBytes()); InputStream is = conn.getInputStream(); byte[] b = new byte[1024]; int len = 0; String s = ""; while((len = is.read(b)) != -1){ String ss = new String(b,0,len,"UTF-8"); s += ss; } System.out.println(s); is.close(); os.close(); conn.disconnect(); } }
復制代碼

 

4.客戶單編程方式(和第一種方式一樣)

復制代碼
//文件名:HelloService.java

import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; import javax.xml.bind.annotation.XmlSeeAlso; import javax.xml.ws.RequestWrapper; import javax.xml.ws.ResponseWrapper; /** * This class was generated by the JAX-WS RI. * JAX-WS RI 2.1.6 in JDK 6 * Generated source version: 2.1 * */ @WebService(name = "HelloService", targetNamespace = "http://ws.itcast.cn/") @XmlSeeAlso({ }) public interface HelloService { /** * * @param arg0 * @return * returns java.lang.String */ @WebMethod @WebResult(targetNamespace = "") @RequestWrapper(localName = "sayHello", targetNamespace = "http://ws.itcast.cn/", className = "cn.itcast.ws.client.SayHello") @ResponseWrapper(localName = "sayHelloResponse", targetNamespace = "http://ws.itcast.cn/", className = "cn.itcast.ws.client.SayHelloResponse") public String sayHello( @WebParam(name = "arg0", targetNamespace = "") String arg0); }
復制代碼

 

復制代碼
import java.net.MalformedURLException; import java.net.URL; import javax.xml.namespace.QName; import javax.xml.ws.Service; import cn.itcast.ws.wsimport.HelloService; /** * 通過客戶端編程的方式調用Webservice服務 * */ public class App { public static void main(String[] args) throws Exception { URL wsdlUrl = new URL("http://192.168.1.100:6789/hello?wsdl"); Service s = Service.create(wsdlUrl, new QName("http://ws.itcast.cn/","HelloServiceService")); HelloService hs = s.getPort(new QName("http://ws.itcast.cn/","HelloServicePort"), HelloService.class); String ret = hs.sayHello("zhangsan"); System.out.println(ret); } }
復制代碼
 

轉自:http://www.cnblogs.com/siqi/archive/2013/12/15/3475222.html

參考:http://blog.csdn.net/dreamcatchergo/article/details/38147165


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM