用JDK開發web service__web

來源:互聯網
上載者:User



伺服器端
    編碼:
        a. 建立一個基於jdk6以上版本的java工程
        b. 定義SEI web service Endpoint interface(web service終端介面)
            @WebService
            public interface HelloWS {
                @WebMethod
                public String sayHello(String name);
            }
        c. 定義SEI的實作類別:
            @WebService
            public class HelloWSImpl implements HelloWS {
                @Override
                public String sayHello(String name) {
                    System.out.println("sayHello "+name);
                    return "hello "+name;
                }
            }

    發布:
        public class Server {
            public static void main(String[] args) {
                //用戶端發送web service請求的url
               String address="http://127.0.0.1/tg_ws/helloWS";
                //處理請求的SEI對象
                HelloWS helloWS = new HelloWSImpl();
                //發布web service
                Endpoint.publish(address, helloWS);
                System.out.println("發布web service成功。");
            }
        }

   (註:我的要建一個ServerDelegate  在Server基礎上點擊 開啟new=》other=》輸入 web service 點擊=》選擇 create web service from java class=》開啟Brouwse 輸入你的測試類別 最後勾上 Generate WSDL in project   會產生一個 * Delegate.java)

②. 用戶端
    1. eclipse Web Service瀏覽器
        a. 查看Web Service所對應的WSDL文檔:...?wsdl
        b. 使用eclipse訪問
            請求體:SOAP Request Envelope
                <soapenv:Envelope>
                    <soapenv:Body>
                        <q0:sayHello>
                            <arg0>tt</arg0>
                        </q0:sayHello>
                    </soapenv:Body>
                </soapenv:Envelope>
            響應體:SOAP Response Envelope
                <S:Envelope>
                    <S:Body>
                        <ns2:sayHelloResponse xmlns:ns2="http://ws.java.atguigu.net/">
                            <return>hello tt</return>
                        </ns2:sayHelloResponse>
                    </S:Body>
                 </S:Envelope>



    2. 編碼實現
        a. 建立用戶端java應用
        b. 在應用的src下執行cxf的命令產生用戶端代碼:
            wsimport -keep http://127.0.0.1/tg_ws/helloWS?wsdl
        c. 編寫用戶端調用的測試代碼,執行:
            public class Client {
                public static void main(String[] args) {
                    //建立SEI的工廠對象
                    HelloWSImplService factory = new HelloWSImplService();
                    //得到一個SEI實作類別對象
                    HelloWSImpl helloWS = factory.getHelloWSImplPort();
                    //調用SEI的方法,此時才去發送web Service請求,並得到返回結果
                    String result = helloWS.sayHello("Tom");
                    System.out.println(result);
                }
            }
                    
        ③.請求過程記錄:使用eclipse的tcp/ip工具進行請求的監控

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.