1. Weather.java
package com.model; public class Weather{ float temperature; String forecast; boolean rain; float howMuchRain; public Weather() {} public Weather(float temperature, String forecast, boolean rain, float howMuchRain) { this.temperature = temperature; this.forecast = forecast; this.rain = rain; this.howMuchRain = 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; } @Override public String toString() { return "{temperature:"+temperature+",forecast:"+forecast+",rain:"+rain+",howMuchRain:"+howMuchRain+"}"; } }
?
?
2. server 端代码
package com.service; import com.model.Weather; public class WeatherService{ Weather weather; /* 特别注意:这里的方法还回类型为void,客户端调用这个方法完成之后, * 在处理还回报文时,会产生一个异常 * org.apache.axis2.AxisFault: The input stream for an incoming message is null. */ public void setWeather(Weather weather){ this.weather = weather; } public Weather getWeather(){ return this.weather; } }?
?
3. 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"> com.service.WeatherService </parameter> </service>?
?
4. 客户端代码
package com.client; import javax.xml.namespace.QName; import org.apache.axiom.om.OMAbstractFactory; import org.apache.axiom.om.OMElement; import org.apache.axiom.om.OMFactory; import org.apache.axis2.AxisFault; import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.client.ServiceClient; import org.apache.axis2.databinding.utils.BeanUtil; import org.apache.axis2.engine.DefaultObjectSupplier; import com.model.Weather; public class TestSoap { private static EndpointReference targetEPR = new EndpointReference( "http://localhost:8888/tservice/services/WeatherService"); public static void main(String[] args) throws AxisFault { Options options = new Options(); options.setTo(targetEPR); // options.setAction("urn:setWeather"); // options.setTransportInProtocol("SOAP"); // options.setProperty(HTTPConstants.CHUNKED, "false"); ServiceClient client = new ServiceClient(); client.setOptions(options); // setWeather client.sendRobust(getBody());//还回类型为void,如果使用sendReceive()会产生异常。 // getWeather OMFactory fac = OMAbstractFactory.getOMFactory(); OMElement ome = fac.createOMElement(new QName("http://service.com", "getWeather")); OMElement result = client.sendReceive(ome); System.out.println(result); OMElement w1 = result.getFirstElement(); Weather w2 = (Weather) BeanUtil.deserialize(Weather.class, w1, new DefaultObjectSupplier(),null); System.out.println(w2.toString()); } private static OMElement getBody() { QName qn1 = new QName("http://service.com", "setWeather"); QName qn2 = new QName("http://service.com", "weather"); Weather w = new Weather((float) 40, "uuuuuuuuu", true, (float) 5.1); OMElement ome = BeanUtil.getOMElement(qn1, new Object[] { w }, qn2, true, null); System.out.println(ome); return ome; } }?
?
5. 运行客户端结果
<setWeather xmlns="http://service.com"><weather><forecast>uuuuuuuuu</forecast><howMuchRain>5.1</howMuchRain><rain>true</rain><temperature>40.0</temperature></weather></setWeather> <ns:getWeatherResponse xmlns:ns="http://service.com"><ns:return xmlns:ax21="http://model.com/xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ax21:Weather"><ax21:forecast>uuuuuuuuu</ax21:forecast><ax21:howMuchRain>5.1</ax21:howMuchRain><ax21:rain>true</ax21:rain><ax21:temperature>40.0</ax21:temperature></ns:return></ns:getWeatherResponse> {temperature:40.0,forecast:uuuuuuuuu,rain:true,howMuchRain:5.1}?