首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 服务器 > Apache >

Apache CXF实战之三 Map门类绑定

2013-07-04 
Apache CXF实战之三Map类型绑定在CXF中,如果Web Service返回类型是Map的时候,比如方法签名如下?[java] vie

Apache CXF实战之三 Map类型绑定

在CXF中,如果Web Service返回类型是Map的时候,比如方法签名如下

?

[java] view plaincopyprint?
  1. @WebMethod??@WebResult?Map<String,?User>?getUserMap();??

此时如果运行程序会得到类似下面的异常

[java] view plaincopyprint?
  1. ...??Caused?by:?com.sun.xml.bind.v2.runtime.IllegalAnnotationsException:?2?counts?of?IllegalAnnotationExceptions??
  2. java.util.Map?is?an?interface,?and?JAXB?can't?handle?interfaces.??????this?problem?is?related?to?the?following?location:??
  3. ????????at?java.util.Map??????????at?private?java.util.Map?com.googlecode.garbagecan.cxfstudy.type.sample2.jaxws_asm.GetUserMapResponse._return??
  4. ????????at?com.googlecode.garbagecan.cxfstudy.type.sample2.jaxws_asm.GetUserMapResponse??java.util.Map?does?not?have?a?no-arg?default?constructor.??
  5. ????this?problem?is?related?to?the?following?location:??????????at?java.util.Map??
  6. ????????at?private?java.util.Map?com.googlecode.garbagecan.cxfstudy.type.sample2.jaxws_asm.GetUserMapResponse._return??????????at?com.googlecode.garbagecan.cxfstudy.type.sample2.jaxws_asm.GetUserMapResponse??
  7. ????at?com.sun.xml.bind.v2.runtime.IllegalAnnotationsException$Builder.check(IllegalAnnotationsException.java:102)??????at?com.sun.xml.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:472)??
  8. ????at?com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:302)??????at?com.sun.xml.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1136)??
  9. ????at?com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:154)??????at?com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:121)??
  10. ????at?sun.reflect.NativeMethodAccessorImpl.invoke0(Native?Method)??????at?sun.reflect.NativeMethodAccessorImpl.invoke(Unknown?Source)??
  11. ????at?sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown?Source)??????at?java.lang.reflect.Method.invoke(Unknown?Source)??
  12. ????at?javax.xml.bind.ContextFinder.newInstance(Unknown?Source)??????at?javax.xml.bind.ContextFinder.newInstance(Unknown?Source)??
  13. ????at?javax.xml.bind.ContextFinder.find(Unknown?Source)??????at?javax.xml.bind.JAXBContext.newInstance(Unknown?Source)??
  14. ????at?org.apache.cxf.jaxb.JAXBDataBinding.createContext(JAXBDataBinding.java:560)??????at?org.apache.cxf.jaxb.JAXBDataBinding.createJAXBContextAndSchemas(JAXBDataBinding.java:500)??
  15. ????at?org.apache.cxf.jaxb.JAXBDataBinding.initialize(JAXBDataBinding.java:320)??????...?25?more??
  16. ...??

如果把方法签名改为如下时

?

?

[java] view plaincopyprint?
  1. @WebMethod??@WebResult?HashMap<String,?User>?getUserMap();??

运行程序会发现返回的结果总是一个空的HashMap。

?

?

对于以上问题,此时需要对Map类型做一下数据绑定或者说转换,看下面详细例子

1. 首先是一个实体类

?

[java] view plaincopyprint?
  1. package?com.googlecode.garbagecan.cxfstudy.type.sample2;????
  2. public?class?User?{??????private?String?id;??
  3. ????private?String?name;??????private?String?password;??
  4. ????public?String?getId()?{??????????return?id;??
  5. ????}??????public?void?setId(String?id)?{??
  6. ????????this.id?=?id;??????}??
  7. ????public?String?getName()?{??????????return?name;??
  8. ????}??????public?void?setName(String?name)?{??
  9. ????????this.name?=?name;??????}??
  10. ????public?String?getPassword()?{??????????return?password;??
  11. ????}??????public?void?setPassword(String?password)?{??
  12. ????????this.password?=?password;??????}??
  13. }??

2. 对Map类型做转换的类和适配器类

?

?

[java] view plaincopyprint?
  1. package?com.googlecode.garbagecan.cxfstudy.type.sample2;????
  2. import?java.util.HashMap;??import?java.util.Map;??
  3. ??import?javax.xml.bind.annotation.adapters.XmlAdapter;??
  4. ??public?class?MapAdapter?extends?XmlAdapter<MapConvertor,?Map<String,?Object>>?{??
  5. ??????@Override??
  6. ????public?MapConvertor?marshal(Map<String,?Object>?map)?throws?Exception?{??????????MapConvertor?convertor?=?new?MapConvertor();??
  7. ????????for?(Map.Entry<String,?Object>?entry?:?map.entrySet())?{??????????????MapConvertor.MapEntry?e?=?new?MapConvertor.MapEntry(entry);??
  8. ????????????convertor.addEntry(e);??????????}??
  9. ????????return?convertor;??????}??
  10. ??????@Override??
  11. ????public?Map<String,?Object>?unmarshal(MapConvertor?map)?throws?Exception?{??????????Map<String,?Object>?result?=?new?HashMap<String,?Object>();??
  12. ????????for?(MapConvertor.MapEntry?e?:?map.getEntries())?{??????????????result.put(e.getKey(),?e.getValue());??
  13. ????????}??????????return?result;??
  14. ????}??}??
  15. ??package?com.googlecode.garbagecan.cxfstudy.type.sample2;??
  16. ??import?java.util.ArrayList;??
  17. import?java.util.List;??import?java.util.Map;??
  18. ??import?javax.xml.bind.annotation.XmlAccessType;??
  19. import?javax.xml.bind.annotation.XmlAccessorType;??import?javax.xml.bind.annotation.XmlType;??
  20. ??@XmlType(name?=?"MapConvertor")??
  21. @XmlAccessorType(XmlAccessType.FIELD)??public?class?MapConvertor?{??
  22. ????private?List<MapEntry>?entries?=?new?ArrayList<MapEntry>();????
  23. ????public?void?addEntry(MapEntry?entry)?{??????????entries.add(entry);??
  24. ????}????
  25. ????public?List<MapEntry>?getEntries()?{??????????return?entries;??
  26. ????}????????
  27. ????public?static?class?MapEntry?{????
  28. ????????private?String?key;????
  29. ????????private?Object?value;????????????
  30. ????????public?MapEntry()?{??????????????super();??
  31. ????????}????
  32. ????????public?MapEntry(Map.Entry<String,?Object>?entry)?{??????????????super();??
  33. ????????????this.key?=?entry.getKey();??????????????this.value?=?entry.getValue();??
  34. ????????}????
  35. ????????public?MapEntry(String?key,?Object?value)?{??????????????super();??
  36. ????????????this.key?=?key;??????????????this.value?=?value;??
  37. ????????}????
  38. ????????public?String?getKey()?{??????????????return?key;??
  39. ????????}????
  40. ????????public?void?setKey(String?key)?{??????????????this.key?=?key;??
  41. ????????}????
  42. ????????public?Object?getValue()?{??????????????return?value;??
  43. ????????}????
  44. ????????public?void?setValue(Object?value)?{??????????????this.value?=?value;??
  45. ????????}??????}??
  46. }??

3. 下面是WebService接口类,注意其中的@XmlJavaTypeAdapter注解部分

?

?

[java] view plaincopyprint?
  1. package?com.googlecode.garbagecan.cxfstudy.type.sample2;????
  2. import?java.util.List;??import?java.util.Map;??
  3. ??import?javax.jws.WebMethod;??
  4. import?javax.jws.WebResult;??import?javax.jws.WebService;??
  5. import?javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;????
  6. @WebService??public?interface?UserService?{??
  7. ??????@WebMethod??
  8. ????@WebResult?List<User>?getUserList();????
  9. ????@WebMethod??????@XmlJavaTypeAdapter(MapAdapter.class)??
  10. ????@WebResult?Map<String,?User>?getUserMap();??}??

4. WebService接口实现类

?

?

[java] view plaincopyprint?
  1. package?com.googlecode.garbagecan.cxfstudy.type.sample2;????
  2. import?java.util.ArrayList;??import?java.util.LinkedHashMap;??
  3. import?java.util.List;??import?java.util.Map;??
  4. ??public?class?UserServiceImpl?implements?UserService?{??
  5. ??????public?List<User>?getUserList()?{??
  6. ????????List<User>?userList?=?new?ArrayList<User>();??????????for?(int?i?=?0;?i?<?10;?i++)?{??
  7. ????????????User?user?=?new?User();??????????????user.setId(""?+?i);??
  8. ????????????user.setName("user_"?+?i);??????????????user.setPassword("password_"?+?i);??
  9. ????????????userList.add(user);??????????}??
  10. ????????return?userList;??????}??
  11. ??????public?Map<String,?User>?getUserMap()?{??
  12. ????????Map<String,?User>?userMap?=?new?LinkedHashMap<String,?User>();??????????for?(int?i?=?0;?i?<?10;?i++)?{??
  13. ????????????User?user?=?new?User();??????????????user.setId(""?+?i);??
  14. ????????????user.setName("user_"?+?i);??????????????user.setPassword("password_"?+?i);??
  15. ????????????userMap.put(user.getId(),?user);??????????}??
  16. ????????return?userMap;??????}??
  17. }??

5. 最后是一个单元测试类

?

?

[java] view plaincopyprint?
  1. package?com.googlecode.garbagecan.cxfstudy.type.sample2;????
  2. import?java.util.List;??import?java.util.Map;??
  3. ??import?javax.xml.ws.Endpoint;??
  4. ??import?org.apache.cxf.jaxws.JaxWsProxyFactoryBean;??
  5. import?org.junit.After;??import?org.junit.Assert;??
  6. import?org.junit.Before;??import?org.junit.BeforeClass;??
  7. import?org.junit.Test;????
  8. public?class?UserServiceTest?{????
  9. ????private?static?final?String?address?=?"http://localhost:9000/ws/type/sample2/userService";????????
  10. ????private?UserService?userService;????????
  11. ????@BeforeClass??????public?static?void?setUpBeforeClass()?throws?Exception?{??
  12. ????????Endpoint.publish(address,?new?UserServiceImpl());??????}??
  13. ??????????@Before??
  14. ????public?void?setUp()?throws?Exception?{??????????JaxWsProxyFactoryBean?factoryBean?=?new?JaxWsProxyFactoryBean();??
  15. ????????factoryBean.setAddress(address);??????????factoryBean.setServiceClass(UserService.class);??
  16. ????????Object?obj?=?factoryBean.create();??????????userService?=?(UserService)obj;??
  17. ????}????
  18. ????@After??????public?void?tearDown()?throws?Exception?{??
  19. ????????userService?=?null;??????}??
  20. ??????@Test??
  21. ????public?void?testGetUserList()?{??????????Assert.assertNotNull(userService);??
  22. ????????List<User>?users?=?userService.getUserList();??????????Assert.assertNotNull(users);??
  23. ????????Assert.assertEquals(10,?users.size());??????}??
  24. ??????@Test??
  25. ????public?void?testGetUserMap()?{??????????Assert.assertNotNull(userService);??
  26. ????????Map<String,?User>?users?=?userService.getUserMap();??????????Assert.assertNotNull(users);??
  27. ????????Assert.assertEquals(10,?users.size());??????}??
  28. ??}??

6. 运行单元测试类验证上面的实现。

热点排行