部署基於JDK的webservice服務類

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   os   ar   java   strong   

部署服務端

兩個註解(@WebService @WebMethod)、一個類(Endpoint)

首先建立JAVA工程ws-server

目錄結構如下

在工程裡建立一個介面,申明一個方法。

package com.atguigu.day01_ws.ws;import javax.jws.WebMethod;import javax.jws.WebService;/* * SEI:  */@WebServicepublic interface HelloWS {    @WebMethod    public String sayHello(String name);}

在建立一個實作類別,實現介面中的方法。

package com.atguigu.day01_ws.ws;import javax.jws.WebService;/* * SEI????? */@WebServicepublic class HelloWSImpl implements HelloWS {    @Override    public String sayHello(String name) {        System.out.println("server sayHello()"+name);        return "Hello " +name;    }}

再發布部署實作類別

package com.atguigu.day01_ws.ws.server;import javax.xml.ws.Endpoint;import com.atguigu.day01_ws.ws.HelloWSImpl;/* * 發布Web Service */public class ServerTest {    public static void main(String[] args) {        //String address = "http://192.168.10.165:8989/day01_ws/hellows";        String address = "http://192.168.107.214:8989/day01_ws/hellows";        Endpoint.publish(address , new HelloWSImpl());        System.out.println("發布webservice成功!");    }}

提示發布成功後,那就瀏覽器訪問http://192.168.107.214:8989/day01_ws/hellows?wsdl
如果出現 wsdl文檔頁面 ,說明發布成功

產生用戶端(藉助jdk的wsimort.exe工具產生用戶端代碼)

建立JAVA工程 ws-client

用cd命令切換到ws-client工程實際目錄下的src目錄下 調用命令 wsimport -keep http://192.168.107.214:8989/day01_ws/hellows?wsdl

會在src目錄下產生相應的用戶端代碼

目錄結構圖如下

最終在當前工程下,建立Test類,

package com.sinosoft.webservice.reFundCallBack;import java.rmi.RemoteException;public class Test {    /**     * @param args     */    public static void main(String[] args) {        HelloWSPortTypeProxy proxy=new HelloWSPortTypeProxy();           try {            System.out.println(proxy.sayHello("大兵哥"));        } catch (RemoteException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }           //System.out.println(proxy.getPrice());           //proxy.printInfo();           //proxy.setName("大兵哥");      }}


最終控制台 輸出 Hello 大兵哥

 

部署基於JDK的webservice服務類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.