發布WebService 1.1

來源:互聯網
上載者:User

標籤:style   class   blog   code   java   http   

 webservice1.1是基於jdk發布的

 1 package cn.itcast.service01; 2  3 import javax.jws.WebService; 4 import javax.xml.ws.Endpoint; 5  6 @WebService 7 public class HelloService { 8      9     public String sayHello(String name)10     {11         System.out.println("say hello called");12         return "hello " +name;13      }14     //main方法不能發布的, 發布的方法不能是靜態 的15     public static void main(String[] args)16     {17         //啟動一個新線程   地址  內容 18         Endpoint.publish("http://192.168.151.42:5678/hello", new HelloService());19         System.out.println("herer ");20     }21 }

 

測試:

一、使用者訪問http://192.168.151.42:5678/hello?wsdl 然後

cmd下輸入  wsimport -s . http://192.168.151.42:5678/hello?wsdl

然後把代碼下下來 去掉其中的*.class

複製代碼到項目測試

在http://192.168.151.42:5678/hello?wsdl中採用從底向上的模式看

 1 package cn.itcast.testService; 2  3 import cn.itcast.service01.HelloService; 4 import cn.itcast.service01.HelloServiceService; 5  6 public class AppTest { 7  8     public static void main(String[] args) 9     {10         /**11          *  wsdl 地址 service name="HelloServiceService" 12          */13         HelloServiceService  service=new HelloServiceService();14 15         HelloService soap=service.getHelloServicePort();16         String str=soap.sayHello("this is sss ");17         System.out.println(str);18     }19 }

這樣 就能實現 在用戶端訪問 服務端的WebService

 

還有一種用戶端寫法實現訪問服務端, 這種方法其實和第一種本質是一樣的,第一種方法採用繼承介面的形式,用戶端拷貝多個檔案。

第二種方法用戶端單單拷貝介面,因為繼承這個介面的構造方法是protected,所以可以用第二種方法,代理對象

二 利用產生的介面 和jdk提供的 Service 等類 實現訪問

伺服器端添加方法

1     public String sayHello2(String name,int n)2     {3         System.out.println("say hello called       --------------------"+n);4         return "hello " +name;5     }

然後 wsimport -s -p cn.itcast.hello  http://192.168.151.42:5678/hello?wsdl

然後單單拷貝產生的介面 HelloService

然後以下代碼

 1 package cn.itcast.testService; 2  3 import java.net.MalformedURLException; 4 import java.net.URL; 5  6 import javax.xml.namespace.QName; 7 import javax.xml.ws.Service; 8  9 import cn.itcast.hello.HelloService;10 11 12 public class AppTest2 {13 14     public static void main(String[] args) throws MalformedURLException15     {                                                                                //命名空間                                                              service16         Service s = Service.create(new URL("http://192.168.151.42:5678/hello?wsdl"), new QName("http://service01.itcast.cn/", "HelloServiceService"));17                                         //命名空間                            服務綁定的port18         HelloService hs = s.getPort(new QName("http://service01.itcast.cn/","HelloServicePort"), HelloService.class);19                             20         String str = hs.sayHello2("lisi",10);21         System.out.println(str);22         System.out.println(hs.getClass().getSimpleName());23     }24 }

就可以訪問了 服務端了

聯繫我們

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