wayne

          WebService (1)

          新建web項目,然后創建一個WEB服務:

          @WebService()
          public class Hello {
              @WebMethod(operationName 
          = "sayHi")
              
          public String sayHi(@WebParam(name = "name")String name) {
                 
          return "Hi " + name;
              }

          }


          可以在源圖上右鍵,選Web服務--添加操作,也可以在設計圖上直接添加操作。@WebService標注表明該類是一個web服務,展現給web服務客戶端的業務方法必須使用@WebMethod標注來表示。打包部署該web應用,web服務自動會發布。可以在glassfish應用服務器上找到該web服務,直接測試或者查看服務器生成的WSDL

          <?xml version="1.0" encoding="UTF-8"?><!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.3.1-hudson-417-SNAPSHOT. --><!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.3.1-hudson-417-SNAPSHOT. -->
          <definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://webservice/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://webservice/" name="HelloService">
              
          <types>
                  
          <xsd:schema>
                      
          <xsd:import namespace="http://webservice/" schemaLocation="http://localhost:8080/WebServiceApp/HelloService?xsd=1">
                      
          </xsd:import>
                  
          </xsd:schema>
              
          </types>
              
          <message name="sayHi">
                  
          <part name="parameters" element="tns:sayHi">
                  
          </part>
              
          </message>
              
          <message name="sayHiResponse">
                  
          <part name="parameters" element="tns:sayHiResponse">
                  
          </part>
              
          </message>
              
          <portType name="Hello">
                  
          <operation name="sayHi">
                      
          <input message="tns:sayHi">
                      
          </input>
                      
          <output message="tns:sayHiResponse">
                      
          </output>
                  
          </operation>
              
          </portType>
              
          <binding name="HelloPortBinding" type="tns:Hello">
                  
          <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document">
                  
          </soap:binding>
                  
          <operation name="sayHi">
                      
          <soap:operation soapAction="">
                      
          </soap:operation>
                      
          <input>
                          
          <soap:body use="literal">
                          
          </soap:body>
                      
          </input>
                      
          <output>
                          
          <soap:body use="literal">
                          
          </soap:body>
                      
          </output>
                  
          </operation>
              
          </binding>
              
          <service name="HelloService">
                  
          <port name="HelloPort" binding="tns:HelloPortBinding">
                      
          <soap:address location="http://localhost:8080/WebServiceApp/HelloService">
                      
          </soap:address>
                  
          </port>
              
          </service>
          </definitions>


          也可以編寫客戶端測試,新建一個普通的java項目,在項目上右鍵,選擇新建--Web服務客戶端,在彈出窗口中指定WebService項目或者WSDL url,點擊完成。在源代碼上右鍵,選擇Web服務客戶端資源--調用Web服務操作,在彈出窗口中選擇sayHi操作,點確定,測試代碼自動生成:

          public class Main {

              
          public static void main(String[] args) {

                  
          try 

                      webservice.HelloService service 
          = new webservice.HelloService();
                      webservice.Hello port 
          = service.getHelloPort();
                      
                      java.lang.String name 
          = "Tom";
                      java.lang.String result 
          = port.sayHi(name);
                      System.out.println(
          "Result = " + result);
                  }
           catch (Exception ex) {
                      
          // TODO handle custom exceptions here
                  }

              }

          }

          運行該客戶端,結果將會輸出

          posted on 2008-09-13 02:31 waynemao 閱讀(165) 評論(0)  編輯  收藏 所屬分類: Java ee


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          My Links

          Blog Stats

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          default

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 襄樊市| 永安市| 宁晋县| 新和县| 那坡县| 五大连池市| 嘉鱼县| 辽宁省| 阿城市| 常熟市| 沅陵县| 张掖市| 卢龙县| 东丰县| 厦门市| 枣阳市| 镇江市| 法库县| 麦盖提县| 河津市| 南汇区| 甘肃省| 全州县| 长岛县| 扎鲁特旗| 汉沽区| 灵山县| 周宁县| 宝兴县| 无锡市| 瑞丽市| 土默特左旗| 泰兴市| 汉川市| 库尔勒市| 井陉县| 鹿泉市| 荆门市| 封丘县| 中卫市| 杂多县|