wayne

          WebService (1)

          新建web項目,然后創(chuàng)建一個WEB服務(wù):

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

          }


          可以在源圖上右鍵,選Web服務(wù)--添加操作,也可以在設(shè)計圖上直接添加操作。@WebService標注表明該類是一個web服務(wù),展現(xiàn)給web服務(wù)客戶端的業(yè)務(wù)方法必須使用@WebMethod標注來表示。打包部署該web應(yīng)用,web服務(wù)自動會發(fā)布。可以在glassfish應(yīng)用服務(wù)器上找到該web服務(wù),直接測試或者查看服務(wù)器生成的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服務(wù)客戶端,在彈出窗口中指定WebService項目或者WSDL url,點擊完成。在源代碼上右鍵,選擇Web服務(wù)客戶端資源--調(diào)用Web服務(wù)操作,在彈出窗口中選擇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
                  }

              }

          }

          運行該客戶端,結(jié)果將會輸出

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


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


          網(wǎng)站導航:
           

          My Links

          Blog Stats

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          default

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 襄垣县| 宜阳县| 五莲县| 枣强县| 漠河县| 攀枝花市| 齐河县| 恭城| 如皋市| 建阳市| 庆元县| 张北县| 丰原市| 昔阳县| 龙游县| 右玉县| 会理县| 庆云县| 嫩江县| 二连浩特市| 尼勒克县| 崇左市| 利辛县| 长白| 鹤峰县| 朝阳市| 巫山县| 安新县| 南川市| 和静县| 广德县| 增城市| 石景山区| 蕉岭县| 亚东县| 和田县| 乌拉特中旗| 永顺县| 泾川县| 科技| 高尔夫|