webservice-demo

webservice開發步驟:java

  服務端開發web

—定義服務接口瀏覽器

—實現服務接口框架

—經過CXF、xfire、axis等框架對外發布服務ide

 客戶端調用url

—經過有效途徑(雙方溝通、UDDI查找等)獲取到Webservice的信息spa

—經過wsdl生成客戶端(或者服務提供方提供對應的接口jar包).net

—調用服務orm

 

service接口:server

  
  
  
  
  1. package com.ws.jaxws.sayHello.service;  
  2.  
  3. import javax.jws.WebService;  
  4. import javax.jws.soap.SOAPBinding;  
  5. import javax.jws.soap.SOAPBinding.Style;  
  6.  
  7. @WebService  
  8. //@SOAPBinding(style=Style.RPC)  
  9. public interface SayHelloService {  
  10.     public String sayHello(String name);  
  11. }  

service接口的實現類:

  
  
  
  
  1. package com.ws.jaxws.sayHello.service.impl;  
  2.  
  3. import javax.jws.WebService;  
  4.  
  5. import com.ws.jaxws.sayHello.service.SayHelloService;  
  6.  
  7. @WebService(endpointInterface = "com.ws.jaxws.sayHello.service.SayHelloService"name = "sayHelloService"targetNamespace = "sayHello")  
  8.  
  9. public class SayHelloServiceImpl implements SayHelloService {  
  10.  
  11.     @Override  
  12.     public String sayHello(String name) {  
  13.         // TODO Auto-generated method stub  
  14.         if("".equals(name)||name==null){  
  15.             name="nobody";  
  16.         }  
  17.         return "hello "+name;  
  18.     }  
  19.  

客戶端:

  
  
  
  
  1. package com.ws.jaxws.sayHello.server;  
  2.  
  3. import javax.xml.ws.Endpoint;  
  4.  
  5. import com.ws.jaxws.sayHello.service.impl.SayHelloServiceImpl;  
  6.  
  7. public class SayHelloServer {  
  8.     public static void main(String[] args) throws Throwable{  
  9.         Endpoint.publish("http://localhost:8888/sayHelloService", new SayHelloServiceImpl());  
  10.         System.out.println("SayHelloService is running....");  
  11.         Thread.sleep(5 * 60 * 1000);  
  12.         System.out.println("time out....");  
  13.         System.exit(0);  
  14.     }  

運行客戶端代碼後在瀏覽器輸入http://localhost:8888/sayHelloServoce?wsdl,便可生成wsdl代碼,以下所示:

wsdl

客戶端調用:

  
  
  
  
  1. package com.ws.jaxws.sayHello.client;  
  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 com.ws.jaxws.sayHello.service.SayHelloService;  
  10.  
  11. public class SayHelloClient {  
  12.     public static void main(String[] args) throws MalformedURLException {  
  13.         String url="http://localhost:8888/sayHelloService?wsdl";  
  14.         Service service=Service.create(new url), new QName("sayHello","SayHelloServiceImplService")); //得到webservice的信息  
  15.         SayHelloService sayHelloService=service.getPort(SayHelloService.class);//return a proxy  
  16.         System.out.println(sayHelloService.sayHello(null));//調用服務  
  17.         System.out.println(sayHelloService.sayHello("zhangsan"));  
  18.     }  

運行客戶端代碼,控制端輸出: hello nobody hello zhangsan

相關文章
相關標籤/搜索