第一步 新建一個webservice interface(注意加入註解包)java
import javax.jws.WebService; @WebService public interface MyService { public int add(int x,int y); public int dec(int x,int y); }
第二步 新建一個webservice interface實現類(注意加入註解包)web
import javax.jws.WebService; @WebService(endpointInterface="com.subnew.ws.MyService") public class MyServiceImpl implements MyService { @Override public int add(int x, int y) { System.out.println(x+"+"+y+"="+(x+y)); return x+y; } @Override public int dec(int x, int y) { System.out.println(x+"-"+y+"="+(x-y)); return x-y; } }
第三步 發佈webservice(注意加入註解包)
ide
import javax.xml.ws.Endpoint; public class ServicePublic { public static void main(String[] args) throws Exception { Endpoint.publish("http://localhost:8083/wcms/", new MyServiceImpl()); } }
試試訪問: http://localhost:8083/wcms/?wsdl 會出現以下界面測試
恭喜你!你經過以上簡單的三步實現你的第一個webservice服務了 !!!url
是否是很簡單。。。spa
咱們能夠經過簡單的代碼測試一下 :.net
import java.net.URL; import javax.xml.namespace.QName; import javax.xml.ws.Service; public class TestClient { public static void main(String[] args) throws Exception { URL url=new URL("http://localhost:8083/wcms/?wsdl"); QName qName=new QName("http://ws.subnew.com/", "MyServiceImplService"); Service service=Service.create(url, qName); MyService my=service.getPort(MyService.class); System.out.println(my.add(1, 2)); } }
看看控制檯輸出吧 。。。code
WebService-wsimport命令使用:
xml
wsimport -d D:/KwSingMV/ -keep -verbose http://localhost:8083/wcms/?wsdl get