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

Cxf 复杂数据类型(Map)

发布时间:2020-12-16 23:32:08 所属栏目:安全 来源:网络整理
导读:?cxf本身不支持复杂数据类型。需要提供一个转换类。 ??? 服务接口 [java] view plain copy @WebService ?? public ? interface ?IHelloService?{?? ?? ???? @WebMethod ?? ????String?sayHello(String?name);?? ?????? ???? @WebResult (name= "listPerson"

?cxf本身不支持复杂数据类型。需要提供一个转换类。

??? 服务接口

[java] view plain copy
  1. @WebService??
  2. public?interface?IHelloService?{??
  3. ??
  4. ????@WebMethod??
  5. ????String?sayHello(String?name);??
  6. ??????
  7. ????@WebResult(name="listPerson")??
  8. ?????????List<Person>?getPersionByName(@WebParam(name="names")String[]?names);??
  9. ?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????@XmlJavaTypeAdapter(MapAdapter.class)??
  10. ?????????Map<String,Person>?getPerson<strong>(@XmlJavaTypeAdapter(MapAdapter.class)??
  11. ?????????Map<String,Person>?person);??
  12. }??
  13. </strong>??


实现类:

?

[html] view plain copy
  1. @WebService(endpointInterface="com.skydream.cxf.IHelloService",serviceName="helloService")??
  2. public?class?HelloServiceImpl?implements?IHelloService???
  3. {??
  4. ????private?final?List<Person>?lstPerson?=?new?ArrayList<Person>();??
  5. ??????
  6. ????private?final?Map<String,Person>?objMap?=?new?HashMap<String,?Person>();??
  7. ??
  8. ????public?HelloServiceImpl()??
  9. ????{??
  10. ????????Person?objPeron1?=?new?Person();??
  11. ????????objPeron1.setId("001");??
  12. ????????objPeron1.setName("skydream");??
  13. ????????objPeron1.setAge(25);??
  14. ????????lstPerson.add(objPeron1);??
  15. ????????objMap.put(objPeron1.getId(),objPeron1);??
  16. ??????????
  17. ????????Person?objPeron2?=?new?Person();??
  18. ????????objPeron2.setId("002");??
  19. ????????objPeron2.setName("xuhaibo");??
  20. ????????objPeron2.setAge(24);??
  21. ????????lstPerson.add(objPeron2);??
  22. ????????objMap.put(objPeron2.getId(),objPeron2);??
  23. ??????????
  24. ????????Person?objPeron3?=?new?Person();??
  25. ????????objPeron3.setId("003");??
  26. ????????objPeron3.setName("xufuyu");??
  27. ????????objPeron3.setAge(25);??
  28. ????????lstPerson.add(objPeron3);??
  29. ????????objMap.put(objPeron2.getId(),objPeron2);??
  30. ????}??
  31. ??????
  32. ????@Override??
  33. ????public?String?sayHello(String?name)???
  34. ????{??
  35. ????????//?TODO?Auto-generated?method?stub??
  36. ????????return?"Hello?"+name+"?;Now?time?is?"+new?Date();??
  37. ????}??
  38. ??
  39. ????@Override??
  40. ????public?List<Person>?getPersionByName(String[]?names)?{??
  41. ??????????
  42. ????????List<Person>?persons?=?new?ArrayList<Person>();??
  43. ????????for(Person?objPerson?:?lstPerson)??
  44. ????????{??
  45. ????????????if(java.util.Arrays.asList(names).contains(objPerson.getName()))??
  46. ????????????{??
  47. ????????????????persons.add(objPerson);??
  48. ????????????}??
  49. ????????}??
  50. ????????return?persons;??
  51. ????}??
  52. ??
  53. ????@Override??
  54. ????public?Map<String,?Person>?getPerson(Map<String,?Person>?person)?{??
  55. ????????person?=?objMap;??
  56. ????????return?person;??
  57. ????}??
  58. ??
  59. }??


?

服务发布应用:

[java] view plain copy
  1. IHelloService?objIHelloService?=?new?HelloServiceImpl();??
  2. ??????
  3. ????CxfPublish?objCxfPublish?=?new?CxfPublish();??
  4. ????objCxfPublish.publishCxf("http://127.0.0.1/helloService",?objIHelloService);??
  5. ????System.out.println("publish?success!");??


?

转换类

?

[java] view plain copy
  1. import?javax.xml.bind.annotation.adapters.XmlAdapter;??
  2. ??
  3. import?com.thoughtworks.xstream.XStream;??
  4. import?com.thoughtworks.xstream.io.xml.DomDriver;??
  5. ??
  6. public?class?MapAdapter?extends?XmlAdapter<String,?Map<String,Person>>?{??
  7. ??
  8. ????@Override??
  9. ????public?Map<String,?Person>?unmarshal(String?v)?throws?Exception?{??
  10. ????????XStream?objXStream?=?new?XStream(new?DomDriver());??
  11. ????????return?(Map<String,?Person>)?objXStream.fromXML(v);??
  12. ????}??
  13. ??
  14. ????@Override??
  15. ????public?String?marshal(Map<String,?Person>?v)?throws?Exception?{??
  16. ????????XStream?objXStream?=?new?XStream();??
  17. ????????return?objXStream.toXML(v);??
  18. ????}??
  19. ??
  20. }??

客户端:

??

[java] view plain copy
  1. JaxWsProxyFactoryBean?objWsProxyFactoryBean?=?new?JaxWsProxyFactoryBean();??
  2. ????????objWsProxyFactoryBean.setAddress("http://127.0.0.1/helloService");??
  3. ????????objWsProxyFactoryBean.setServiceClass(IHelloService.class);??
  4. ????????IHelloService?objHelloService?=?(IHelloService)objWsProxyFactoryBean.create();??
  5. ??????????
  6. ????????String?strResult?=?objHelloService.sayHello("skydream");??
  7. ????????System.out.println("client?sahHello?result?:"+strResult);??
  8. ??????????
  9. ????????List<Person>?lstPerson?=?objHelloService.getPersionByName(new?String[]{"skydream","xuhaibo","god"});??
  10. ????????System.out.println("This?is?the?getPersonByName?result?:"?+?lstPerson);??
  11. ??????????
  12. ????????Map<String,Person>?objMap?=?objHelloService.getPerson(new?HashMap<String,?Person>());??
  13. ????????System.out.println(objMap);??


说明:转换类可以自己实现,本文使用了xstream.jar提供的XStream实现对象和String类型。
@XmlJavaTypeAdapter(MapAdapter.class)是标明该处需要进行转换,转换过的工具方法是MapAdapter.

该类必须是XmlAdapter<ValueType,BoundType>

其中ValueType是转换后Cxf能够支持的对象。

BoundType是需要转换的对象。

(编辑:李大同)

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

    推荐文章
      热点阅读