第一個簡單的webService

來源:互聯網
上載者:User

標籤:

 

1.介面

 1 package com.seecen.service; 2  3 import javax.jws.WebParam; 4 import javax.jws.WebService; 5  6 @WebService 7 public interface IWebService { 8      9     String sayHi(@WebParam(name="text")String text,@WebParam(name="age")int age);    // 參數的名字叫text10 11 }

2.實現

 1 package com.seecen.service.impl; 2  3 import javax.jws.WebService; 4  5 import com.seecen.service.IWebService; 6  7 @WebService(endpointInterface ="com.seecen.service.IWebService",serviceName="testService") 8 public class WebServiceImpl implements IWebService{ 9 10     public String sayHi(String text, int age) {11         return "思成" + text + age;12     }13 14 }

-------伺服器端虛擬測試---------------------------------

 1 package main; 2   3   4 import javax.xml.ws.Endpoint; 5   6 import com.seecen.service.IWebService; 7 import com.seecen.service.impl.WebServiceImpl; 8   9  10 public class TestService {11  12     public static void main(String[] args) {13         System.out.println("Web Service start");14         IWebService webService = new WebServiceImpl();15         String address = "http://192.168.1.137:8080/web";16         Endpoint.publish(address,webService);               // 發布服務地址和介面17         System.out.println("Web Service started");18     }19  20 }

----------------------------------------------------------------------

用戶端使用:

找到 apache-cxf-3.0.1 存放的路徑,找到其下的 bin

(在 cmd ---- F:----cd apache-cxf-3.0.1---cd bin (提前將com檔案刪掉)---wsdl2java‘空格’-frontend jaxws21 (url)http://192.168.1.137:8080/web?wsdl )

 1 public class Test { 2 ?  3     /** 4      * @param args 5      */ 6     public static void main(String[] args) { 7   8         JaxWsProxyFactoryBean svr = new JaxWsProxyFactoryBean();    //通過JaxWsProxyFactoryBean動態產生用戶端代理 9         svr.setServiceClass(IWebService.class);                     //傳入服務介面10         svr.setAddress("http://192.168.1.137:8080/web");            //發布服務的地址11         IWebService hw = (IWebService)svr.create();                 //然後調用create()方法建立動態代理12  13         System.out.println(hw.sayHi("翠花", 18));14  15     }16  17 }

 

第一個簡單的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.