实战CXF调用Webxml天气预报服务

 
http://my.oschina.net/bayer/blog/52490

实战CXF调用Webxml天气预报服务

今晚群里的kasasis同学问我会不会使用CXF调用

http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl

这个天气预报的WebService服务;

我也是初学者, 在尝试调用的过程中发现不少问题, 也有一些小心得, 希望与大家分享, 更希望大家可以帮我解惑.

一.初探:通过wsdl2java生成调用远程服务所需要的java类
因为还没有成功使用dynamic-clients(http://cxf.apache.org/docs/dynamic-clients.html)的方式动态调用过WebService服务, 所以决定使用CXF提供的wsdl2java来生成调用WebService服务所必须的java类, 进而使用JaxWsServerFactoryBean来调用WebService服务:

遗憾的是, CXF wsdl2java报错了:
undefined element declaration 's:schema'
at line 85 column 41 of schema 

http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl

将该wsdl下载下来, 使用MyEclipse 9.0打开并查看, 也提示了这一错误:
src-resolve.4.2: Error resolving component 's:schema'.

It was detected that 's:schema' is in namespace 'http://www.w3.org/2001/XMLSchema', but components from this namespace are not referenceable from schema document 'file:///D:/workspace/workspace_j2ee/CXFWeatherTest/src/WeatherWebService.asmx.original.wsdl'.

If this is the incorrect namespace, perhaps the prefix of 's:schema' needs to be changed.

If this is the correct namespace, then an appropriate 'import' tag should be added to 'file:///D:/workspace/workspace_j2ee/CXFWeatherTest/src/WeatherWebService.asmx.original.wsdl'.

wsdl报错部分内容如下(下边代码红色部分):
Xml代码  收藏代码
  1. <s:element name="getSupportDataSetResponse">  
  2.     <s:complexType>  
  3.         <s:sequence>  
  4.             <s:element  
  5.                 minOccurs="0"  
  6.                 maxOccurs="1"  
  7.                 name="getSupportDataSetResult">  
  8.                 <s:complexType>  
  9.                     <s:sequence>  
  10.                         <s:element ref="s:schema" />  
  11.                         <s:any />  
  12.                     </s:sequence>  
  13.                 </s:complexType>  
  14.             </s:element>  
  15.         </s:sequence>  
  16.     </s:complexType>  
  17. </s:element>  


作为初学者, 曾在风中叶老师的视频中学习过关于schema及DTD的一些知识, 但还没修炼到能解决这一错误的程度, 也期待各位同学的积极探索并给出答案;

问题催人奋进, 折衷的办法总是有的:

<s:any/>既然可以代表任何元素, 为什么不能代表<s:element ref="xxx"/>元素呢? 于是乎, 愤然删除<s:element ref="s:schema" />, 勉强算是通过了CXF提供的wsdl验证工具wsdlvalidator的验证;

Ok, 继续之前的步骤, 通过wsdl2java来生成调用远程WebService服务所需要的java类;

一切顺利, 生成的类的目录结构如下:

 

二.使用JaxWsProxyFactoryBean调用天气服务
本工程使用的jar包如下图示:



通过JaxWsProxyFactoryBean调用webService服务的过程相信大家都很熟悉了, 具体代码如下:
Java代码  收藏代码
  1. package cn.com.client;  
  2. import java.util.List;  
  3. import org.apache.cxf.interceptor.LoggingInInterceptor;  
  4. import org.apache.cxf.interceptor.LoggingOutInterceptor;  
  5. import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;  
  6. import cn.com.webxml.ArrayOfString;  
  7. import cn.com.webxml.WeatherWebServiceSoap;  
  8. public class Test {  
  9.     public static void main(String[] args) throws Exception {  
  10.         JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();  
  11.         factory.getInInterceptors().add(new LoggingInInterceptor());  
  12.         factory.getOutInterceptors().add(new LoggingOutInterceptor());  
  13.         factory.setAddress(  
  14.                 "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx");  
  15.         factory.setServiceClass(WeatherWebServiceSoap.class);  
  16.         WeatherWebServiceSoap client =  
  17.                 (WeatherWebServiceSoap) factory.create();  
  18.         ArrayOfString o = client.getWeatherbyCityName("南阳");  
  19.         List<String> strList = o.getString();  
  20.         for (String str : strList) {  
  21.             System.out.println(str);  
  22.         }  
  23.     }  
  24. }  


运行还算顺利, 得到的结果如下:

信息: Outbound Message

---------------------------

ID: 1

Address: http://www.webxml.com.cn/WebServices/WeatherWebService.asmx

Encoding: UTF-8

Content-Type: text/xml

Headers: {Accept=[*/*], SOAPAction=["http://WebXml.com.cn/getWeatherbyCityName"]}

Payload:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">

<soap:Body>

<getWeatherbyCityName xmlns="http://WebXml.com.cn/">

<theCityName>南阳</theCityName>

</getWeatherbyCityName>

</soap:Body>

</soap:Envelope>

河南

南阳

57178

57178.jpg

2012-4-5 22:58:11

11℃/24℃

4月6日 晴

无持续风向微风

0.gif

0.gif

今日天气实况:气温:16℃;风向/风力:北风 0级;湿度:53%;空气质量:较差;紫外线强度:中等

穿衣指数:建议着薄型套装等春秋过渡装。年老体弱者宜着套装。但昼夜温差较大,注意适当增减衣服。

感冒指数:昼夜温差较大,较易发生感冒,请适当增减衣服。体质较弱的朋友请注意防护。

运动指数:天气较好,赶快投身大自然参与户外运动,尽情感受运动的快乐吧。

洗车指数:适宜洗车,未来持续两天无雨天气较好,适合擦洗汽车,蓝天白云、风和日丽将伴您的车子连日洁净。

晾晒指数:天气不错,适宜晾晒。赶紧把久未见阳光的衣物搬出来吸收一下太阳的味道吧!

旅游指数:天气晴朗,风和日丽,温度适宜,是个好天气哦。这样的天气很适宜旅游,您可以尽情地享受大自然的风光。

路况指数:天气较好,路面比较干燥,路况较好。

舒适度指数:温度适宜,风力不大,您在这样的天气条件下,会感到比较清爽和舒适。

空气污染指数:气象条件较不利于空气污染物稀释、扩散和清除,请适当减少室外活动时间。

紫外线指数:属中等强度紫外线辐射天气,外出时建议涂擦SPF高于15、PA+的防晒护肤品,戴帽子、太阳镜。

11℃/24℃

4月7日 晴

无持续风向微风

0.gif

0.gif

10℃/23℃

4月8日 阴转多云

无持续风向微风

2.gif

1.gif

南阳市位于河南省西南部,北靠伏牛山,东扶桐柏山,西依秦岭,南临汉江。..................


三.使用JaxWsDynamicClientFactory调用天气服务
CXF关于Dynamic-clients的介绍文档见:

http://cxf.apache.org/docs/dynamic-clients.html

很简洁的一段官方示例代码:
Java代码  收藏代码
  1. JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();  
  2.    
  3. Client client = dcf.createClient("echo.wsdl");  
  4.    
  5. Object[] res = client.invoke("echo""test echo");  
  6.    
  7. System.out.println("Echo response: " + res[0]);  

但具体的调用过程并非一帆风顺; 让人不解的是, 即使调用自己所创建的WebService, 使用动态方式调用也不能成功, 总会提示一个异常:
Exception in thread "main" java.lang.IllegalStateException: Unable to write generated Java files for schemas: null

at org.apache.cxf.endpoint.dynamic.DynamicClientFactory.createClient(DynamicClientFactory.java:351)

at org.apache.cxf.endpoint.dynamic.DynamicClientFactory.createClient(DynamicClientFactory.java:234)

at org.apache.cxf.endpoint.dynamic.DynamicClientFactory.createClient(DynamicClientFactory.java:227)

at org.apache.cxf.endpoint.dynamic.DynamicClientFactory.createClient(DynamicClientFactory.java:182)

at com.client.Test.dynamicStudent(Test.java:30)

at com.client.Test.main(Test.java:38)

Caused by: java.lang.reflect.UndeclaredThrowableException

at $Proxy29.build(Unknown Source)

at org.apache.cxf.endpoint.dynamic.DynamicClientFactory.createClient(DynamicClientFactory.java:349)

... 5 more

Caused by: java.lang.NoSuchMethodException:

com.sun.codemodel.internal.JCodeModel.build(java.lang.Object)

at java.lang.Class.getMethod(Class.java:1605)

at org.apache.cxf.common.util.ReflectionInvokationHandler.invoke(ReflectionInvokationHandler.java:50)

... 7 more
异常信息的最后一个Caused by给了我线索:

java.lang.NoSuchMethodException:

com.sun.codemodel.internal.JCodeModel.build(java.lang.Object)

遍历工程所用jar文件, 居然没有找到internal包下的类JCodeModel;

但报的异常是NoSuchMethodException, 而不是ClassNotFoundException, 表明该类是存在的, 可能包路径发生了变化, 遂STFG(search the fUcking google. 从陈皓老师的博文学到的 - - 囧), 最终发现该类现存在于jaxb-xjc-2.2.4-1.jar中, 包路径为com.sun.codemodel, 而不是异常中所提示的com.sun.codemodel.internal;

为什么会这样? 难道是我的jaxb-xjc jar包的版本不对?

遂通过http://jaxb.java.net/, 对比jaxb2.0.1 -- 2.2.5之间的所有版本, 均没有找到包com.sun.codemodel.internal...

Ok, 让我们继续查找jaxb1.x版本的jar包;

幸运的是, 在页面[url]http://jaxb.java.net/nonav/2.0.1/docs/jaxb-1_0.html [/url], 终于有所发现:

JAXB 2.0 is backwards compatible with JAXB 1.0 - you can deploy your existing 1.0 applications on the 2.0 runtime (provided that you also bundle the jaxb1-impl.jar) and they should run without modification.

查看我的项目的类库, 刚好缺少了这么一个jaxb-impl.jar包,,, 加上该包(我使用的是jaxb-impl-2.2.4-1.jar), 再次运行动态调用天气预报WebService的服务, Congratulations! 这个异常被干掉了!

但是, 不要高兴的太早, 服务并没有调用成功, 因为又回到了最开始遇到的问题:

<s:element ref="s:schema" />,

undefined element declaration 's:schema'

at line 85 column 41 of schema

http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl

具体调用代码如下:
Java代码  收藏代码
  1. package com.client;  
  2. import org.apache.cxf.endpoint.Client;  
  3. import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;  
  4. public class Test {  
  5.     public static void main(String[] args) throws Exception {  
  6.         JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();  
  7.         Client client = dcf.createClient(  
  8.             "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl");  
  9.         Object[] res = client.invoke("getSupportProvince");  
  10.         System.out.println("Echo response: " + res[0]);  
  11.     }  
  12. }  


值得欣慰的是, 在补充了jaxb-impl-2.2.4-1.jar之后, 可以通过动态方式成功调用自己创建的WebService服务了, 详见下节;

四.使用JaxWsDynamicClientFactory动态调用自己创建的WebService服务
服务接口及实现类很简单:

接口 com.service.StudentService:
Java代码  收藏代码
  1. package com.service;  
  2. import javax.jws.WebParam;  
  3. import javax.jws.WebService;  
  4. @WebService  
  5. public interface StudentService {  
  6.     String helloStudent(@WebParam(name="text")String name);  
  7. }  

实现类 com.service.impl.StudentServiceImpl:
Java代码  收藏代码
  1. package com.service.impl;  
  2. import javax.jws.WebService;  
  3. import com.service.StudentService;  
  4. @WebService(endpointInterface="com.service.StudentService", targetNamespace="http://service.com/")  
  5. public class StudentServiceImpl implements StudentService{  
  6.     public String helloStudent(String name) {  
  7.         return "hello " + name;  
  8.     }  
  9. }  

server类: com.jettyServer.ServerForJetty:
Java代码  收藏代码
  1. package com.jettyServer;  
  2. import javax.xml.ws.Endpoint;  
  3. import com.service.impl.StudentServiceImpl;  
  4. public class ServerForJetty {  
  5.     public static void main(String[] args) throws InterruptedException {  
  6.         StudentServiceImpl implementor = new StudentServiceImpl();  
  7.         String address = "http://localhost:9000/student";  
  8.         Endpoint.publish(address, implementor);  
  9.     }  
  10. }  

client类:com.client.Test
Java代码  收藏代码
  1. package com.client;  
  2. import org.apache.cxf.endpoint.Client;  
  3. import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;  
  4. public class Test {  
  5.     public static void main(String[] args) throws Exception {  
  6.         JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();  
  7.         Client client = dcf.createClient("http://localhost:9000/student?wsdl");  
  8.         Object[] res = client.invoke("helloStudent""LeeThinker");  
  9.         System.out.println("Echo response: " + res[0]);  
  10.     }  
  11. }  

先启动JettyServer, 后访问http://localhost:9000/student?wsdl, 成功刷新出wsdl, Ok, 服务顺利启动!

再执行Test的main方法, Congratulations! 这次真正使用Dynamic的方式, 在不需要通过wsdl2java生成客户端java类文件的情况下, 成功调用WebService服务!

至于不能通过这种方式成功调用

http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl

的问题, 还需要在进一步学习Schema和DTD的知识才能解答;

五.小结:
虽未能通过Dynamic的方式成功调用天气预报服务, 好歹wsdl2java生成客户端调用类

的方式是可行的.

六.其他问题:
6.1.@WebService声明问题
在"四.使用JaxWsDynamicClientFactory调用自己创建的WebService服务"中

自定义服务接口的实现类com.service.impl.StudentServiceImpl的类声明之前, 仅注释声明了@WebService, 而没有后边的(endpointInterface="com.service.StudentService",

targetNamespace="http://service.com/"), 这时使用动态代理方式调用该服务时, 会发生异常:

Exception in thread "main"

org.apache.cxf.common.i18n.UncheckedException: No operation was  found with the name {http://impl.service.com/}helloStudent.

大概还是因为WSDL的基础知识不够过硬吧, STFG后, 在

http://lost-alien.iteye.com/blog/1175859 找到答案:



在com.service.impl.StudentServiceImpl的类声明之前, 正确注释了

@WebService(

endpointInterface="com.service.StudentService",  targetNamespace="http://service.com/")之后,

再使用动态方式调用该服务, 一切顺利!

具体原因还有待分析, 认识和理解!

6.2.关于CXF wsdl2java生成的调用远程服务的客户端java类文件
在"四.使用JaxWsDynamicClientFactory调用自己创建的WebService服务"中所描述的自定义WebService服务中, 若使用JaxWsServerFactoryBean的调用方式, 那么首先需要通过wsdl2java生成客户端必需的java类文件;

将通过自己的wsdl生成的客户端java类文件的目录结构&类名称,

和wsdl2java http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl

这个wsdl文件后生成的java类文件的目录结构&类名称, 对比之后, 发现差距明显:



对比可以清楚的看到, 自己的wsdl生成的客户端类文件, 其java类文件名及其混乱, 始终没能弄懂为什么会这样? 还请各位同学多多指教!
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章