首先,建立一个WebService:
package garfield;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
@WebService
public class MyJ6WebService {
public String SayHello(String strName) {
return "Hello ,"+strName+"!";
}
public static void main(String[] args) {
//发布WebService,注意如果提示:Exception in thread "main" com.sun.xml.internal.ws.server.ServerRtException: Server Runtime Error: java.net.BindException: Address already in use: bind
//则需要修改一下发布端口
Endpoint.publish("http://localhost:8030/garfield.MyJ6WebService", new MyJ6WebService());
System.out.println("WebService was published success !");
}
}
启动Tomcat,然后运行,系统会输出:WebService was published success !,表示WebService已启动。
显示WebService信息:
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
在WebService启动的状态下,在命令行中输入:
E:\Temp>wsimport -p garfield.garfieldj6wsclient -keep http://localhost:8030/garf
ield.MyJ6WebService?wsdl
parsing WSDL...
generating code...
compiling code...
E:\Temp>
注意,一定要对应你发布的WebService信息!
然后系统会自动生成相关的接口文件,如下:
新建Java工程,将输出文件引入,然后建立测试类:
package xxh;
import garfield.garfieldj6wsclient.*;
public class WebClient {
/**
* @param args
*/
public static void main(String[] args) {
//创建一个客户端服务对象
MyJ6WebService myJ6WS = new MyJ6WebServiceService().getMyJ6WebServicePort();
//调用服务方法,并得到方法返回值
String strTest = myJ6WS.sayHello("Garfield");
//打印服务的返回值
System.out.println(strTest);
}
}
在WebService运行情况下,运行客户端测试程序,输出:
Hello ,Garfield!