SOAP得Axis2的使用(一)create client code
首页
http://ws.apache.org/axis2/
create client guide
http://ws.apache.org/axis2/1_5_1/userguide-creatingclients.html#createclients
拷贝下载的axis2到工作目录,配置
AXIS2_HOME=F:\book\opensource\axis2\axis2-1.5.1
然后在命令行里面输入
wsdl2java -help
就看到很多参数,我具体使用的如下:
wsdl2java -uri http://192.168.1.149:8080/easyaxis/services/WeatherService?wsdl -o ./ -p com.sillycat.easycxfclient -t
其中
-uri 指明了WSDL文件的路径
-o 指明了生成文件的路径
-p 指明了生成的包名
-t 生成了测试用例,我用来先测试验证一下的:)
查看了一下测试用例里面的代码,WeatherServiceTest.java,构造stub,然后通过stub调用WS
com.sillycat.easycxfclient.WeatherServiceStub stub =
new com.sillycat.easycxfclient.WeatherServiceStub();//the default implementation should point to the right endpoint
com.sillycat.easycxfclient.WeatherServiceStub.SetWeather setWeather10=
(com.sillycat.easycxfclient.WeatherServiceStub.SetWeather)getTestObject(com.sillycat.easycxfclient.WeatherServiceStub.SetWeather.class);
stub.setWeather(setWeather10);
仔细看了一下WeatherServiceStub.java这个生成的CODE里面的代码,里面的构造函数可以传递endpoint的地址,这样也就比较灵活了:
public WeatherServiceStub(java.lang.String targetEndpoint) throws org.apache.axis2.AxisFault {
this(null,targetEndpoint);
}
为了以后生成代码方便,不用每次都去open一个cmd窗口,所以我看了一下wsdl2java.bat里面的脚本,发现它调用的是这个类:
org.apache.axis2.wsdl.WSDL2Java
的main方法。另外要使用这个类,需要先倒入这个JAR:
<dependency org="org/apache/axis2" name="axis2-codegen" rev="1.5.1" />
<dependency org="org/apache/axis2" name="axis2-adb-codegen" rev="1.5.1" />
我自己写了个简单的JAVA APP去调用WSDL2Java,代码如下GenerateJavaFromWsdl.java:
package com.sillycat.gen;
import org.apache.axis2.wsdl.WSDL2Java;
public class GenerateJavaFromWsdl {
public static void main(String[] args){
StringBuffer sb = new StringBuffer();
sb.append("-uri http://192.168.1.149:8080/easyaxis/services/WeatherService?wsdl ");
sb.append("-o E:/work/easyaxisweather/ ");
sb.append("-p com.sillycat.axis2.weather.client");
try {
WSDL2Java.main(sb.toString().split(" "));
} catch (Exception e) {
e.printStackTrace();
}
}
}
我的测试用例WeatherServiceTest.java:
@Test
public void weatherServiceStub() throws RemoteException {
String url = "http://192.168.1.149:8080/easyaxis/services/WeatherService.WeatherServiceHttpSoap12Endpoint/";
WeatherServiceStub stub = null;
stub = new WeatherServiceStub(url);
// set weather
SetWeather setWeather = new SetWeather();
com.sillycat.axis2.weather.client.WeatherServiceStub.Weather weather = new com.sillycat.axis2.weather.client.WeatherServiceStub.Weather();
weather.setTemperature((float) 39.3);
weather.setForecast("Cloudy with showers");
weather.setRain(true);
weather.setHowMuchRain((float) 4.5);
setWeather.setWeather(weather);
stub.setWeather(setWeather);
// get weather
GetWeatherResponse result = stub.getWeather();
com.sillycat.axis2.weather.client.WeatherServiceStub.Weather result_weather = result
.get_return();
// Displaying the result
System.out.println("Temperature : "
+ result_weather.getTemperature());
System.out.println("Forecast : "
+ result_weather.getForecast());
System.out.println("Rain : "
+ result_weather.getRain());
System.out.println("How much rain (in inches) : "
+ result_weather.getHowMuchRain());
}
其实发现,这种方式去调用webservice,其实也不必API的方式麻烦,也是一个很好用的方法。呵呵。
首页
http://ws.apache.org/axis2/
create client guide
http://ws.apache.org/axis2/1_5_1/userguide-creatingclients.html#createclients
拷贝下载的axis2到工作目录,配置
AXIS2_HOME=F:\book\opensource\axis2\axis2-1.5.1
然后在命令行里面输入
wsdl2java -help
就看到很多参数,我具体使用的如下:
wsdl2java -uri http://192.168.1.149:8080/easyaxis/services/WeatherService?wsdl -o ./ -p com.sillycat.easycxfclient -t
其中
-uri 指明了WSDL文件的路径
-o 指明了生成文件的路径
-p 指明了生成的包名
-t 生成了测试用例,我用来先测试验证一下的:)
查看了一下测试用例里面的代码,WeatherServiceTest.java,构造stub,然后通过stub调用WS
com.sillycat.easycxfclient.WeatherServiceStub stub =
new com.sillycat.easycxfclient.WeatherServiceStub();//the default implementation should point to the right endpoint
com.sillycat.easycxfclient.WeatherServiceStub.SetWeather setWeather10=
(com.sillycat.easycxfclient.WeatherServiceStub.SetWeather)getTestObject(com.sillycat.easycxfclient.WeatherServiceStub.SetWeather.class);
stub.setWeather(setWeather10);
仔细看了一下WeatherServiceStub.java这个生成的CODE里面的代码,里面的构造函数可以传递endpoint的地址,这样也就比较灵活了:
public WeatherServiceStub(java.lang.String targetEndpoint) throws org.apache.axis2.AxisFault {
this(null,targetEndpoint);
}
为了以后生成代码方便,不用每次都去open一个cmd窗口,所以我看了一下wsdl2java.bat里面的脚本,发现它调用的是这个类:
org.apache.axis2.wsdl.WSDL2Java
的main方法。另外要使用这个类,需要先倒入这个JAR:
<dependency org="org/apache/axis2" name="axis2-codegen" rev="1.5.1" />
<dependency org="org/apache/axis2" name="axis2-adb-codegen" rev="1.5.1" />
我自己写了个简单的JAVA APP去调用WSDL2Java,代码如下GenerateJavaFromWsdl.java:
package com.sillycat.gen;
import org.apache.axis2.wsdl.WSDL2Java;
public class GenerateJavaFromWsdl {
public static void main(String[] args){
StringBuffer sb = new StringBuffer();
sb.append("-uri http://192.168.1.149:8080/easyaxis/services/WeatherService?wsdl ");
sb.append("-o E:/work/easyaxisweather/ ");
sb.append("-p com.sillycat.axis2.weather.client");
try {
WSDL2Java.main(sb.toString().split(" "));
} catch (Exception e) {
e.printStackTrace();
}
}
}
我的测试用例WeatherServiceTest.java:
@Test
public void weatherServiceStub() throws RemoteException {
String url = "http://192.168.1.149:8080/easyaxis/services/WeatherService.WeatherServiceHttpSoap12Endpoint/";
WeatherServiceStub stub = null;
stub = new WeatherServiceStub(url);
// set weather
SetWeather setWeather = new SetWeather();
com.sillycat.axis2.weather.client.WeatherServiceStub.Weather weather = new com.sillycat.axis2.weather.client.WeatherServiceStub.Weather();
weather.setTemperature((float) 39.3);
weather.setForecast("Cloudy with showers");
weather.setRain(true);
weather.setHowMuchRain((float) 4.5);
setWeather.setWeather(weather);
stub.setWeather(setWeather);
// get weather
GetWeatherResponse result = stub.getWeather();
com.sillycat.axis2.weather.client.WeatherServiceStub.Weather result_weather = result
.get_return();
// Displaying the result
System.out.println("Temperature : "
+ result_weather.getTemperature());
System.out.println("Forecast : "
+ result_weather.getForecast());
System.out.println("Rain : "
+ result_weather.getRain());
System.out.println("How much rain (in inches) : "
+ result_weather.getHowMuchRain());
}
其实发现,这种方式去调用webservice,其实也不必API的方式麻烦,也是一个很好用的方法。呵呵。