Axis2常用部署方式【WEB】
项目代码:
services.xml
<service name="WeatherService" scope="application"> <description> Weather POJO Service </description> <messageReceivers> <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only" class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver"/> <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out" class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/> </messageReceivers> <parameter name="ServiceClass">sample.pojo.service.WeatherService</parameter> </service>
WeatherService:
package sample.pojo.service; import sample.pojo.data.Weather; public class WeatherService{ Weather weather; public void setWeather(Weather weather){ this.weather = weather; } public Weather getWeather(){ return this.weather; } }
Weather:
package sample.pojo.data; public class Weather{ float temperature; String forecast; boolean rain; float howMuchRain; public void setTemperature(float temp){ temperature = temp; } public float getTemperature(){ return temperature; } public void setForecast(String fore){ forecast = fore; } public String getForecast(){ return forecast; } public void setRain(boolean r){ rain = r; } public boolean getRain(){ return rain; } public void setHowMuchRain(float howMuch){ howMuchRain = howMuch; } public float getHowMuchRain(){ return howMuchRain; } }
客户端代码:
package sample.pojo.rpcclient; import javax.xml.namespace.QName; import org.apache.axis2.AxisFault; import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.rpc.client.RPCServiceClient; import sample.pojo.data.Weather; public class WeatherRPCClient { public static void main(String[] args1) throws AxisFault { RPCServiceClient serviceClient = new RPCServiceClient(); Options options = serviceClient.getOptions(); EndpointReference targetEPR = new EndpointReference("http://192.168.87.222:8080/webservice/services/WeatherService"); options.setTo(targetEPR); serviceClient.setOptions(options); // Setting the weather QName opSetWeather = new QName("http://service.pojo.sample/xsd", "setWeather"); Weather w = new Weather(); w.setTemperature((float) 39.3); w.setForecast("Cloudy with showers"); w.setRain(true); w.setHowMuchRain((float) 4.5); Object[] opSetWeatherArgs = new Object[] {w}; serviceClient.invokeRobust(opSetWeather, opSetWeatherArgs); // Getting the weather QName opGetWeather = new QName("http://service.pojo.sample/xsd", "getWeather"); Object[] opGetWeatherArgs = new Object[] {}; Class[] returnTypes = new Class[] {Weather.class}; Object[] response = serviceClient.invokeBlocking(opGetWeather, opGetWeatherArgs, returnTypes); Weather result = (Weather) response[0]; if (result == null) { System.out.println("Weather didn't initialize!"); return; } // Displaying the result System.out.println("Temperature : " + result.getTemperature()); System.out.println("Forecast : " + result.getForecast()); System.out.println("Rain : " + result.getRain()); System.out.println("How much rain (in inches) : " + result.getHowMuchRain()); } }
1,文件夹方式
文件夹包结构:
WEB-INF\services\WeatherService
\META-INF\services.xml
\sample\pojo\service\WeatherService.class
\sample\pojo\data\Weather.class
按照此结构将编译后的java代码放置在web中,即可实现webservice的部署。
2,打包.aar方式
此种方式就是将方式#1的代码打成.aar包,然后放置到WEB-INF\services\。
a,打开CMD命令行,切换到WEB-INF\services\WeatherService,键入:
E:\workspace\webservice\src\main\webapp\WEB-INF\services\WeatherService>jar cvf WeatherService.aar .
b,打包没问题时屏幕出现下面的字幕:
E:\workspace\webservice\src\main\webapp\WEB-INF\services\WeatherService>jar cvf WeatherService.aar . 标明清单(manifest) 忽略项 META-INF/ 增加:META-INF/services.xml(读入= 587) (写出= 238)(压缩了 59%) 增加:sample/(读入= 0) (写出= 0)(存储了 0%) 增加:sample/pojo/(读入= 0) (写出= 0)(存储了 0%) 增加:sample/pojo/data/(读入= 0) (写出= 0)(存储了 0%) 增加:sample/pojo/data/Weather.class(读入= 727) (写出= 372)(压缩了 48%) 增加:sample/pojo/service/(读入= 0) (写出= 0)(存储了 0%) 增加:sample/pojo/service/WeatherService.class(读入= 358) (写出= 220)(压缩了 38%)
c,此时到WEB-INF\services\WeatherService下就有一个名为WeatherService.aar 的文件。由于与文件夹内容相同,所以运行tomcat前需要把WEB-INF\services\WeatherService\WeatherService.aar拷贝到WEB-INF\services下,并需要把WEB-INF\services\WeatherService文件夹删除。
3,以编程方式部署
通过这种方式可以将services.xml和webservice的代码放到任意java classpath下。
4,以传统Java对象POJO(Plain Old Java Object)部署
生成Stub代码:
a,进入CMD命令行,切换目录到%AXIS2_HOME%\bin\,然后键入:wsdl2java
D:\Buckup\Apache Axis2\1.2\axis2-1.2\axis2-1.2\bin>wsdl2java Using AXIS2_HOME: D:\Buckup\Apache Axis2\1.2\axis2-1.2\axis2-1.2 Using JAVA_HOME: D:\Java\jdk1.6.0_34 Usage: WSDL2Java -uri <url or path> : A url or path to a WSDL -o <path> Specify a directory path for the generated code. -a Generate async style code only (Default: off). -s Generate sync style code only (Default: off). Takes precedence over -a. -p <pkg1> Specify a custom package name for the generated code. -l <language> Valid languages are java and csharp (Default: java). -t Generate a test case for the generated code. -ss Generate server side code (i.e. skeletons) (Default: off). -sd Generate service descriptor (i.e. services.xml). (Default: off). Valid with -ss. -d <databinding> Valid databinding(s) are adb, xmlbeans, jibx, jaxme and jaxbri (Default: adb). -g Generates all the classes. Valid only with -ss. -pn <port_name> Choose a specific port when there are multiple ports in the wsdl. -sn <service_name> Choose a specific service when there are multiple services in the wsdl. -u Unpacks the databinding classes -r <path> Specify a repository against which code is generated. -ns2p ns1=pkg1,ns2=pkg2 Specify a custom package name for each namespace specified in the wsdls schema. -ssi Generate an interface for the service implementation (Default: off). -wv WSDL Version. Valid Options : 2, 2.0, 1.1 -S Specify a directory path for generated source -R Specify a directory path for generated resources -em Specify an external mapping file -f Flattens the generated files -uw Switch on un-wrapping. -xsdconfig <path to file> Use XMLBeans .xsdconfig file. Valid only with -d xmlbeans.
b,上面展示了wsdl2java的使用方式,如果你已经完全了解,则忽略上述步骤。键入:
wsdl2java -uri http://192.168.87.222:8080/webservice/services/WeatherService?wsdl -p sample.pojo -o C:/webservice -url:指定WSDL -p:指定生成代码的java包名 -o:指定存放生成代码的文件夹
c,完成后到C:\webservice\src\sample\pojo下可以查看生成的文件,由于生成文件数量众多,就不贴出来了。