axis2 bin包里的源码使用ant生成的arr发布使用客户端调用服务正常,输出:
Temperature : 39.3
Forecast : Cloudy with showers
Rain : true
How much rain (in inches) : 4.5
而使用Axis2的Service Achiever插件生成的arr发布调用失败,输出:
Weather didn't initialize!//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.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;
}
}//这是客户端package sample.pojo.rpcclient;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;import javax.xml.namespace.QName;
public class WeatherRPCClient {
public static void main(String[] args1) throws AxisFault {
RPCServiceClient serviceClient = new RPCServiceClient(); Options options = serviceClient.getOptions(); EndpointReference targetEPR = new EndpointReference(
"http://localhost:8080/axis2/services/WeatherService"); options.setTo(targetEPR); // Setting the weather
QName opSetWeather = new QName("http://service.pojo.sample",
"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",
"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());
}
}
Temperature : 39.3
Forecast : Cloudy with showers
Rain : true
How much rain (in inches) : 4.5
而使用Axis2的Service Achiever插件生成的arr发布调用失败,输出:
Weather didn't initialize!//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.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;
}
}//这是客户端package sample.pojo.rpcclient;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;import javax.xml.namespace.QName;
public class WeatherRPCClient {
public static void main(String[] args1) throws AxisFault {
RPCServiceClient serviceClient = new RPCServiceClient(); Options options = serviceClient.getOptions(); EndpointReference targetEPR = new EndpointReference(
"http://localhost:8080/axis2/services/WeatherService"); options.setTo(targetEPR); // Setting the weather
QName opSetWeather = new QName("http://service.pojo.sample",
"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",
"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());
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货