Search code examples
javaserializationsoaptreemap

Use TreeMap with SOAP in Java


I am trying to return a TreeMap in a method in my SOAP service, obtaining the following exception:

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$1.createJAXBContext(Unknown Source)
    at com.sun.xml.internal.ws.model.AbstractSEIModelImpl$1.run(Unknown Source)
    at com.sun.xml.internal.ws.model.AbstractSEIModelImpl$1.run(Unknown Source)
    ... 12 more

Why is it trying to serialize a List? Can I make it work with any other SortedMap?


Solution

  • As pointed out by @PaulVargas in the comments, TreeMap is not directly supported by JAX-B.

    I resolved this issue creating my TreeMapSerializer adapting this answer JAXB: How to suppress surrounding XmlElement when using XmlJavaTypeAdapter?