当前位置: 代码迷 >> Java Web开发 >> web service 第一天接触不懂 仍是那个访问天气预报的例子 代码很简单
  详细解决方案

web service 第一天接触不懂 仍是那个访问天气预报的例子 代码很简单

热度:3655   发布时间:2013-02-25 21:09:27.0
web service 第一天接触不懂 还是那个访问天气预报的例子 代码很简单
Java code
public class WetherReport {    public static void main(String[] args) {        Service service = new Service();        try {            Call call = (Call) service.createCall();            call.setTargetEndpointAddress(new URL("http://www.ayandy.com/Service.asmx?WSDL"));             call.setUseSOAPAction(true);            call.setSOAPActionURI("http://tempuri.org/getSupportProvince");                           String [] weatherReport2 = (String[])call.invoke("getSupportProvince", new String[]{"all"});            System.out.println(weatherReport2);        } catch (ServiceException e) {            e.printStackTrace();        } catch (MalformedURLException e) {            e.printStackTrace();        } catch (AxisFault e) {            e.printStackTrace();        }    }    }

报错如下:
org.xml.sax.SAXException: SimpleDeserializer encountered a child element, which is NOT expected, in something it was trying to deserialize.
at org.apache.axis.encoding.ser.SimpleDeserializer.onStartChild(SimpleDeserializer.java:145)
at org.apache.axis.encoding.DeserializationContext.startElement(DeserializationContext.java:1035)
at org.apache.axis.message.SAX2EventRecorder.replay(SAX2EventRecorder.java:165)
at org.apache.axis.message.MessageElement.publishToHandler(MessageElement.java:1141)
at org.apache.axis.message.RPCElement.deserialize(RPCElement.java:345)
at org.apache.axis.message.RPCElement.getParams(RPCElement.java:384)
at org.apache.axis.client.Call.invoke(Call.java:2467)
at org.apache.axis.client.Call.invoke(Call.java:2366)
at org.apache.axis.client.Call.invoke(Call.java:2391)
at com.cfd.testMath.WetherReport.main(WetherReport.java:24)
希望大家指点一下! 谢谢 。我是新手

------解决方案--------------------------------------------------------
我都是用 CXF 的,我和你说一下 CXF 怎么调用吧。

1:到 http://cxf.apache.org/download 去下载最新的 Apache CXF 包,下载好后解压到某个目录中,比如:d:/apache/cxf-2.1.4 中;

2:在控制台中进入 d:/apache/cxf-2.1.4/bin 目录,执行这个命令:

wsdl2java -p com.bao.ws -d d:/ws -verbose http://www.ayandy.com/Service.asmx?WSDL

wsdl2java 命令会生成一些封装的类和 SOAP 接口。

-p 参数是个包名,表示生成的类、接口放在什么包下面
-d 是个目录名,生成的包放在什么目录下
-verbose 是在生成代码时产生一些日志输出
最后一个是 WSDL 地址

3:生成好后把这些文件复制到 Eclipse 某个工程的相同包下面去。

4:到 CXF 的 lib 目录中把这些文件加载到 Eclipse 的 classpath 中去:

cxf-2.2.3.jar
geronimo-activation_1.1_spec-1.0.2.jar
geronimo-annotation_1.0_spec-1.1.1.jar
geronimo-jaxws_2.1_spec-1.0.jar
geronimo-stax-api_1.0_spec-1.0.1.jar
geronimo-ws-metadata_2.0_spec-1.1.2.jar
jaxb-api-2.1.jar
jaxb-impl-2.1.12.jar
saaj-api-1.3.jar
wsdl4j-1.6.2.jar
wstx-asl-3.2.8.jar
XmlSchema-1.4.5.jar

4:写个 Test 类测试一下:

Java code
import javax.xml.namespace.QName;import javax.xml.ws.Service;import javax.xml.ws.soap.SOAPBinding;public class WebServiceTest {    private static final QName SERVICE_NAME = new QName("http://tempuri.org/", "ServiceSoap");    private static final QName PORT_NAME = new QName("http://tempuri.org/", "ServiceSoapPort");        public static void main(String[] args) {        Service service = Service.create(SERVICE_NAME);        String endpointAddress = "http://www.ayandy.com/Service.asmx";        service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING, endpointAddress);        ServiceSoap hw = service.getPort(ServiceSoap.class);        ArrayOfString as = hw.getWeatherbyCityName("上海", TheDayFlagEnum.TODAY);        for(String str : as.getString()) {            System.out.println(str);        }    }}
------解决方案--------------------------------------------------------
顶龙果兄~
  相关解决方案