2014/2/23
先建立要透過 Web Service 呼叫的 method
package tw.shunyi.ws.server;
import javax.jws.WebService;
@WebService
public class CircleFunctions {
public double getArea(double r) {
return java.lang.Math.PI * (r * r);
}
public double getCircumference(double r) {
return 2 * java.lang.Math.PI * r;
}
}
建立 server 端程式
package tw.shunyi.ws.server;
import javax.xml.ws.Endpoint;
public class Server {
public static void main(String[] args) {
Endpoint.publish(
"http://localhost:8080/SimpleWebService/circlefunctions",
new CircleFunctions());
}
}
啟動 web server
3.1. 程式會動態產生 wrapper class
2014/2/23 上午 09:11:19 com.sun.xml.internal.ws.model.RuntimeModeler getRequestWrapperClass
資訊: Dynamically creating request wrapper Class tw.shunyi.ws.server.jaxws.GetArea
2014/2/23 上午 09:11:19 com.sun.xml.internal.ws.model.RuntimeModeler getResponseWrapperClass
資訊: Dynamically creating response wrapper bean Class tw.shunyi.ws.server.jaxws.GetAreaResponse
2014/2/23 上午 09:11:19 com.sun.xml.internal.ws.model.RuntimeModeler getRequestWrapperClass
資訊: Dynamically creating request wrapper Class tw.shunyi.ws.server.jaxws.GetCircumference
2014/2/23 上午 09:11:19 com.sun.xml.internal.ws.model.RuntimeModeler getResponseWrapperClass
資訊: Dynamically creating response wrapper bean Class tw.shunyi.ws.server.jaxws.GetCircumferenceResponse
3.2. 可以執行以下指令來產生 wrapper class, 避免每次重啟 server 都要動態產生
wsgen -verbose -cp . tw.shunyi.ws.server.CircleFunctions
執行以下指令產生給 client site 用的檔案
wsimport -p tw.shunyi.ws.client -s src -d bin -verbose http://localhost:8080/SimpleWebService/circlefunctions?WSDL
建立 client 端程式
package tw.shunyi.ws.client;
public class Client {
private static CircleFunctionsService service;
public static void main(String[] args) {
service = new CircleFunctionsService();
CircleFunctions port = service.getCircleFunctionsPort();
System.out.println(port.getArea(1.0));
}
}
執行結果
3.141592653589793