日期:2014-05-16  浏览次数:20798 次

Apache CXF实战之三 Map类型绑定

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

?

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

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

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

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

?

?