在 Java 中将 TreeMap 与 SOAP 结合使用

Use TreeMap with SOAP in Java

我正在尝试在我的 SOAP 服务的方法中 return TreeMap,获得以下异常:

Exception in thread "main" javax.xml.ws.WebServiceException: Unable to create JAXBContext
    at com.sun.xml.internal.ws.model.AbstractSEIModelImpl.createJAXBContext(Unknown Source)
    at com.sun.xml.internal.ws.model.AbstractSEIModelImpl.postProcess(Unknown Source)
    at com.sun.xml.internal.ws.model.RuntimeModeler.buildRuntimeModel(Unknown Source)
    at com.sun.xml.internal.ws.server.EndpointFactory.createSEIModel(Unknown Source)
    at com.sun.xml.internal.ws.server.EndpointFactory.createEndpoint(Unknown Source)
    at com.sun.xml.internal.ws.api.server.WSEndpoint.create(Unknown Source)
    at com.sun.xml.internal.ws.transport.http.server.EndpointImpl.createEndpoint(Unknown Source)
    at com.sun.xml.internal.ws.transport.http.server.EndpointImpl.publish(Unknown Source)
    at com.sun.xml.internal.ws.spi.ProviderImpl.createAndPublishEndpoint(Unknown Source)
    at javax.xml.ws.Endpoint.publish(Unknown Source)
    at webservice.soap.ServicePublisher.main(ServicePublisher.java:15)
Caused by: java.security.PrivilegedActionException: com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
java.util.List is an interface, and JAXB can't handle interfaces.
    this problem is related to the following location:
        at java.util.List
        at public java.util.TreeMap dictionary.jaxws.GetWordsBegginingWithResponse._return
        at dictionary.jaxws.GetWordsBegginingWithResponse

    at java.security.AccessController.doPrivileged(Native Method)
    ... 11 more
Caused by: com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
java.util.List is an interface, and JAXB can't handle interfaces.
    this problem is related to the following location:
        at java.util.List
        at public java.util.TreeMap dictionary.jaxws.GetWordsBegginingWithResponse._return
        at dictionary.jaxws.GetWordsBegginingWithResponse

    at com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException$Builder.check(Unknown Source)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(Unknown Source)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(Unknown Source)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(Unknown Source)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(Unknown Source)
    at com.sun.xml.internal.bind.v2.ContextFactory.createContext(Unknown Source)
    at com.sun.xml.internal.bind.api.JAXBRIContext.newInstance(Unknown Source)
    at com.sun.xml.internal.ws.developer.JAXBContextFactory.createJAXBContext(Unknown Source)
    at com.sun.xml.internal.ws.model.AbstractSEIModelImpl.run(Unknown Source)
    at com.sun.xml.internal.ws.model.AbstractSEIModelImpl.run(Unknown Source)
    ... 12 more

为什么要尝试序列化 List?我可以让它与任何其他 SortedMap 一起使用吗?

正如@PaulVargas 在评论中指出的那样,JAX-B 不直接支持TreeMap

我解决了这个问题创建我的 TreeMapSerializer 适应这个答案 JAXB: How to suppress surrounding XmlElement when using XmlJavaTypeAdapter?