概念性東東
webservice目的:異構平台之間的交互
JAX-WS:Java Api XML Web Service(基於JAVAAPI XML實現的WebService)
SEI:Service Endpoint Interface(實例中的IMyService)
SIB:Service Implements Bean(實例中的MyServiceImpl)
一個簡單的WebService實例
1 服務器的建立
1.1 創建接口
package com.lul.service; import javax.jws.WebService; @WebService() public interface IMyService { public int add(int a, int b); public int minus(int a, int b); }
1.2 創建實現類
package com.lul.service; import javax.jws.WebService; @WebService(endpointInterface="com.lul.service.IMyService") public class MyServiceImpl implements IMyService { @Override public int add(int a, int b) { System.out.println(a+"+"+b+"="+(a+b)); return (a+b); } @Override public int minus(int a, int b) { System.out.println(a+"-"+b+"="+(a-b)); return (a-b); } }
1.3 開啟服務
package com.lul.service; import javax.xml.ws.Endpoint; public class MyService { public static void main(String[] args){ String address="http://localhost:8888/ns"; Endpoint.publish(address,new MyServiceImpl()); } }
2 客戶端的建立
package com.lul.service; import java.net.MalformedURLException; import java.net.URL; import javax.xml.namespace.QName; import javax.xml.ws.Service; public class TestClient { public static void main(String[] args) { try { URL url=new URL("http://localhost:8888/ns?wsdl"); QName sname=new QName("http://service.lul.com/","MyServiceImplService"); Service service=Service.create(url, sname); IMyService ms=service.getPort(IMyService.class); System.out.println(ms.add(10, 9)); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }運行結果: