加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

webservice学习之处理Map等CXF无法自动转化的类型

发布时间:2020-12-16 22:11:23 所属栏目:安全 来源:网络整理
导读:from: http://blog.csdn.net/wlsyn/article/details/8756068 CXF形参、返回值 ? ? ? 1. 当形参和返回值的类型是String、基本数据类型是,CXF肯定可以轻松处理 ? ? ? ?2.当形参和返回值的类型是javabean式的复合类(就是普通的POJO实体类)、List集合、数组等

from:

http://blog.csdn.net/wlsyn/article/details/8756068

CXF形参、返回值
? ? ? 1. 当形参和返回值的类型是String、基本数据类型是,CXF肯定可以轻松处理
? ? ? ?2.当形参和返回值的类型是javabean式的复合类(就是普通的POJO实体类)、List集合、数组等复杂类型时, CXF也可以很好处理。
? ? ? ?3.还有一些像Map、非javabean式的复合类,CXF是处理不了的

如果遇到系统无法自动处理的类型,就需要程序员自行处理,方法是提供一个转化器,该转化器负责把CXF不能处理的类型,转化为CXF能够处理的类型,具体过程如下:
(1) 使用注解 @XmlJavaTypeAdapter(java自身的注解,可在jdkAPI文档中查到)修饰CXF无法自动处理的类型,使用该Annotation时,通过value属性指定一个转换器(自己定义)。
@XmlJavaTypeAdapter (value="MyXmlAdapter.class")?

(2) 实现自己定义的转化器,实现转化器时,需要开发一个CXF能够处理的类型。

1. 注解 @XmlJavaTypeAdapter标识返回值为Map的接口

[html] view plain copy
  1. @WebService??
  2. public?interface?HelloWorld?{??
  3. ??????
  4. ????@XmlJavaTypeAdapter((XmlMapAdapter.class))?Map<String,?String>?getSpace(String?deviceIp);??
  5. ??????
  6. }??
[html] view plain copy
  1. ??
[html] view plain copy
  1. ??
实现类保持不变:
[html] view plain copy
  1. @Component("hello")??
  2. @WebService(endpointInterface?=?"demo.spring.service.HelloWorld")??
  3. public?class?HelloWorldImpl?implements?HelloWorld?{??
  4. ??
  5. ??
  6. ??
  7. ??
  8. ????public?Map<String,?String>?getSpace(String?deviceIp)?{??
  9. ????????//?TODO?Auto-generated?method?stub??
  10. ????????HashMap<String,?String>?test?=?new?HashMap<String,String>();??
  11. ????????test.put("test",??"10.5");??
  12. ????????test.put("ip",?deviceIp);??
  13. ??????????
  14. ????????System.out.println("deviceIp:?"?+?deviceIp);??
  15. ??????????
  16. ????????return?test;??
  17. ????}??
  18. }??


2.定义自行创建的XmlMapAdapter类型
[html] view plain copy
  1. public?class?XmlMapAdapter?extends?XmlAdapter<MyStringMap,?Map<String,?String>>?{??
  2. ??
  3. ????@Override??
  4. ????public?Map<String,?String>?unmarshal(MyStringMap?v)?throws?Exception?{??
  5. ????????//?TODO?Auto-generated?method?stub??
  6. ??????????
  7. ????????Map<String,?String>?result?=?new?HashMap<String,?String>();??
  8. ??????????
  9. ????????for?(Entry?entry?:?v.getEntries())?{??
  10. ????????????result.put(entry.getKey(),?entry.getValue());??
  11. ????????}??
  12. ??????????
  13. ????????return?result;??
  14. ????}??
  15. ??
  16. ????@Override??
  17. ????public?MyStringMap?marshal(Map<String,?String>?v)?throws?Exception?{??
  18. ????????//?TODO?Auto-generated?method?stub??
  19. ????????MyStringMap?msm?=?new?MyStringMap();??
  20. ????????List<Entry>?eList?=?new?ArrayList<Entry>();??
  21. ????????for(String?key?:?v.keySet())?{??
  22. ??????????????
  23. ????????????Entry?entry?=?new?Entry();??
  24. ????????????entry.setKey(key);??
  25. ????????????entry.setValue(v.get(key));??
  26. ????????????eList.add(entry);??
  27. ????????}??
  28. ????????msm.setEntries(eList);??
  29. ????????return?msm;??
  30. ????}??
  31. ??
  32. ??????
  33. ??
  34. }??
通过继承 XmlAdapter<ValueType,BoundType>类型,便可已将CXF不能处理的类型进行转换。
jdkAPI中定义如下,valuType是能够处理的类型,boundType是不能处理的类型:
转化的实质是将不能处理的类型,如Map,将其值取出,赋予另一个实体类,这个类模拟Map,保存他的值,这样便是可以进行相互转化。为此,需要定义一个Map的模拟类,这样Map的key和value都保存在Entry类中(Entry自行定义,名字也可以随便,只要符合命名规范就行),所有的Entry保存在List中,这样一个Map集合就转化成了MyStringMap类,MyStringMap自然也可以转化为Map类:
[html] view plain copy
  1. public?class?MyStringMap?{??
  2. ??????
  3. ????private?List<Entry>?entries;??
  4. ??
  5. ????/**??
  6. ?????*?@return?entries??
  7. ?????*/??
  8. ????public?List<Entry>?getEntries()?{??
  9. ????????return?entries;??
  10. ????}??
  11. ??
  12. ????/**??
  13. ?????*?@param?entries?the?entries?to?set??
  14. ?????*/??
  15. ????public?void?setEntries(List<Entry>?entries)?{??
  16. ????????this.entries?=?entries;??
  17. ????}??
  18. ??????
  19. ????public?static?class?Entry?{??
  20. ????????private?String?key;??
  21. ????????private?String?value;??
  22. ????????/**??
  23. ?????????*?@return?key??
  24. ?????????*/??
  25. ????????public?String?getKey()?{??
  26. ????????????return?key;??
  27. ????????}??
  28. ????????/**??
  29. ?????????*?@param?key?the?key?to?set??
  30. ?????????*/??
  31. ????????public?void?setKey(String?key)?{??
  32. ????????????this.key?=?key;??
  33. ????????}??
  34. ????????/**??
  35. ?????????*?@return?value??
  36. ?????????*/??
  37. ????????public?String?getValue()?{??
  38. ????????????return?value;??
  39. ????????}??
  40. ????????/**??
  41. ?????????*?@param?value?the?value?to?set??
  42. ?????????*/??
  43. ????????public?void?setValue(String?value)?{??
  44. ????????????this.value?=?value;??
  45. ????????}??
  46. ??????????
  47. ??????????
  48. ????}???
  49. ??????
  50. }??


avax.xml.bind.annotation.adapters

Class XmlAdapter<ValueType,BoundType>

  • java.lang.Object
  • javax.xml.bind.annotation.adapters.XmlAdapter<ValueType,BoundType>
  • Type Parameters:
    BoundType?- The type that JAXB doesn't know how to handle. An adapter is written to allow this type to be used as an in-memory representation through the? ValueType.
    ValueType?- The type that JAXB knows how to handle out of the box.

3.部署项目到tomcat中,启动,如能访问到WSDL文件,WSDL发布成功。

4.使用命令生成客户端,具体方法见博文。

5.测试客户端:
[html] view plain copy
  1. public?static?void?main(String?[]args)?{??
  2. ???????HelloWorldImplService?service?=?new?HelloWorldImplService();??
  3. ?????????
  4. ???????HelloWorld?hw?=?service.getHelloWorldImplPort();??
  5. ?????????
  6. ???????MyStringMap?msm?=?hw.getSpace("");??
  7. ?????????
  8. ???????List<Entry>?entries?=?msm.getEntries();??
  9. ?????????
  10. ???????for?(Entry?e?:?entries)?{??
  11. ???????????System.out.println("key:?"?+?e.getKey()?+?"?"?+?"value:?"?+?e.getValue());??
  12. ???????}??
  13. ???}??

结果如下:
[html] view plain copy
  1. 2013-4-3?15:56:19?org.apache.cxf.service.factory.ReflectionServiceFactoryBean?buildServiceFromWSDL??
  2. 信息:?Creating?Service?{http://service.spring.demo/}HelloWorldImplService?from?WSDL:?http://192.168.1.133:8088/CXFUseCase/services/helloWorld?wsdl??
  3. key:?test?value:?10.5??
  4. key:?ip?value:?192.168.3.51??
至此,转化操作完成。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读