1. W3School中关于webservice的讲解。
http://www.w3school.com.cn/ws.asp
2. java中实现Webservice。
(1)新建项目TheServer,编写server端的java类:
package com.scb.simon.service; import javax.jws.WebService; import javax.xml.ws.Endpoint; @WebService public class ServiceHello { public String sayhello(String name){ return "Hello, " + name; } public static void main(String[] args) { // TODO Auto-generated method stub Endpoint.publish("http://localhost:8081/service/serviceHello", new ServiceHello()); System.out.println("service bind successfully."); } }
运行,打印"service bind successfully."说明绑定服务成功。
通过http://localhost:8081/service/serviceHello?wsdl测试是否能够正确拿到wsdl。
(2)新建TheClient项目,然后cmd命令行下调用java的wsimport命令。
wsimport -s D:/workplace/graduationDesign/TheClient/src -p com.scb.simon.client -keep http://localhost:8081/service/serviceHello?wsdl
然后刷新项目,就可以看到生成的代码了。
(3)编写测试代码如下:
package com.scb.simon.test; import com.scb.simon.client.ServiceHello; import com.scb.simon.client.ServiceHelloService; public class TestService { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub ServiceHello hello = new ServiceHelloService().getServiceHelloPort(); System.out.println(hello.sayhello("Simon")); } }
console打印:Hello, Simon,说明测试成功。