webservice開發步驟:java
服務端開發web
—定義服務接口瀏覽器
—實現服務接口框架
—經過CXF、xfire、axis等框架對外發布服務ide
客戶端調用url
—經過有效途徑(雙方溝通、UDDI查找等)獲取到Webservice的信息spa
—經過wsdl生成客戶端(或者服務提供方提供對應的接口jar包).net
—調用服務orm
service接口:server
- package com.ws.jaxws.sayHello.service;
- import javax.jws.WebService;
- import javax.jws.soap.SOAPBinding;
- import javax.jws.soap.SOAPBinding.Style;
- @WebService
- //@SOAPBinding(style=Style.RPC)
- public interface SayHelloService {
- public String sayHello(String name);
- }
service接口的實現類:
- package com.ws.jaxws.sayHello.service.impl;
- import javax.jws.WebService;
- import com.ws.jaxws.sayHello.service.SayHelloService;
- @WebService(endpointInterface = "com.ws.jaxws.sayHello.service.SayHelloService", name = "sayHelloService", targetNamespace = "sayHello")
- public class SayHelloServiceImpl implements SayHelloService {
- @Override
- public String sayHello(String name) {
- // TODO Auto-generated method stub
- if("".equals(name)||name==null){
- name="nobody";
- }
- return "hello "+name;
- }
- }
客戶端:
- package com.ws.jaxws.sayHello.server;
- import javax.xml.ws.Endpoint;
- import com.ws.jaxws.sayHello.service.impl.SayHelloServiceImpl;
- public class SayHelloServer {
- public static void main(String[] args) throws Throwable{
- Endpoint.publish("http://localhost:8888/sayHelloService", new SayHelloServiceImpl());
- System.out.println("SayHelloService is running....");
- Thread.sleep(5 * 60 * 1000);
- System.out.println("time out....");
- System.exit(0);
- }
- }
運行客戶端代碼後在瀏覽器輸入http://localhost:8888/sayHelloServoce?wsdl,便可生成wsdl代碼,以下所示:
客戶端調用:
- package com.ws.jaxws.sayHello.client;
- import java.net.MalformedURLException;
- import java.net.URL;
- import javax.xml.namespace.QName;
- import javax.xml.ws.Service;
- import com.ws.jaxws.sayHello.service.SayHelloService;
- public class SayHelloClient {
- public static void main(String[] args) throws MalformedURLException {
- String url="http://localhost:8888/sayHelloService?wsdl";
- Service service=Service.create(new url), new QName("sayHello","SayHelloServiceImplService")); //得到webservice的信息
- SayHelloService sayHelloService=service.getPort(SayHelloService.class);//return a proxy
- System.out.println(sayHelloService.sayHello(null));//調用服務
- System.out.println(sayHelloService.sayHello("zhangsan"));
- }
- }
運行客戶端代碼,控制端輸出: hello nobody hello zhangsan