Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8157233

java.util.Map cannot be marshalled if annotated with @XmlElement

XMLWordPrintable

    • generic
    • generic

      FULL PRODUCT VERSION :
      sstory$ java -version
      java version "1.8.0_25"
      Java(TM) SE Runtime Environment (build 1.8.0_25-b17)
      Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      OS X El Capitan 10.11.3 (15D21)

      A DESCRIPTION OF THE PROBLEM :
      Before determining that this was a bug, I posted my problem here: http://stackoverflow.com/questions/35275239/marshall-unmarshall-nested-map-with-jaxb/35321306#35321306

      In recap: If using JAXB annotations to marshall a POJO, and that POJO contains a java.util.Map field, annotating that field with @XmlElement results in:

      java.lang.ExceptionInInitializerError
      at com.digitalreasoning.datajetway.models.chat.ChatSession.<init>(ChatSession.groovy:24)
      at com.digitalreasoning.datajetway.models.chat.ChatSession$ChatBuilder.build(ChatSession.groovy:144)
      at com.digitalreasoning.datajetway.models.chat.ChatSessionTest.setup(ChatSessionTest.groovy:51)
      Caused by: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 2 counts of IllegalAnnotationExceptions
      java.util.Map is an interface, and JAXB can't handle interfaces.
      this problem is related to the following location:
      at java.util.Map
      at private java.util.Map MyField.someMap
      at Customer
      java.util.Map does not have a no-arg default constructor.
      this problem is related to the following location:
      at java.util.Map
      at private java.util.Map MyField.someMap
      at Customer
      at com.sun.xml.bind.v2.runtime.IllegalAnnotationsException$Builder.check(IllegalAnnotationsException.java:106)
      at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:489)
      at com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:319)
      at com.sun.xml.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1170)
      at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:145)
      at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:247)
      at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:234)
      at javax.xml.bind.ContextFinder.find(ContextFinder.java:441)
      at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:641)
      at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:584)
      ... 3 more

      Omitting the annotation (and therefore accepting the default name, required=false, etc) results in correct marshalling.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      The code sample needed to reproduce is in the question here: http://stackoverflow.com/questions/35275239/marshall-unmarshall-nested-map-with-jaxb/35321306#35321306

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
      <customer>
        <myField>
          <someMap>
            <entry>
              <key>foo</key>
              <value>bar</value>
            </entry>
            <entry>
              <key>baz</key>
              <value>qux</value>
            </entry>
          </someMap>
        </myField>
      </customer>
      ACTUAL -
      java.lang.ExceptionInInitializerError
      at com.digitalreasoning.datajetway.models.chat.ChatSession.<init>(ChatSession.groovy:24)
      at com.digitalreasoning.datajetway.models.chat.ChatSession$ChatBuilder.build(ChatSession.groovy:144)
      at com.digitalreasoning.datajetway.models.chat.ChatSessionTest.setup(ChatSessionTest.groovy:51)
      Caused by: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 2 counts of IllegalAnnotationExceptions
      java.util.Map is an interface, and JAXB can't handle interfaces.
      this problem is related to the following location:
      at java.util.Map
      at private java.util.Map MyField.someMap
      at Customer
      java.util.Map does not have a no-arg default constructor.
      this problem is related to the following location:
      at java.util.Map
      at private java.util.Map MyField.someMap
      at Customer
      at com.sun.xml.bind.v2.runtime.IllegalAnnotationsException$Builder.check(IllegalAnnotationsException.java:106)
      at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:489)
      at com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:319)
      at com.sun.xml.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1170)
      at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:145)
      at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:247)
      at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:234)
      at javax.xml.bind.ContextFinder.find(ContextFinder.java:441)
      at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:641)
      at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:584)
      ... 3 more

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      java.lang.ExceptionInInitializerError
      at com.digitalreasoning.datajetway.models.chat.ChatSession.<init>(ChatSession.groovy:24)
      at com.digitalreasoning.datajetway.models.chat.ChatSession$ChatBuilder.build(ChatSession.groovy:144)
      at com.digitalreasoning.datajetway.models.chat.ChatSessionTest.setup(ChatSessionTest.groovy:51)
      Caused by: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 2 counts of IllegalAnnotationExceptions
      java.util.Map is an interface, and JAXB can't handle interfaces.
      this problem is related to the following location:
      at java.util.Map
      at private java.util.Map MyField.someMap
      at Customer
      java.util.Map does not have a no-arg default constructor.
      this problem is related to the following location:
      at java.util.Map
      at private java.util.Map MyField.someMap
      at Customer
      at com.sun.xml.bind.v2.runtime.IllegalAnnotationsException$Builder.check(IllegalAnnotationsException.java:106)
      at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:489)
      at com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:319)
      at com.sun.xml.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1170)
      at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:145)
      at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:247)
      at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:234)
      at javax.xml.bind.ContextFinder.find(ContextFinder.java:441)
      at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:641)
      at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:584)
      ... 3 more

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      http://stackoverflow.com/questions/35275239/marshall-unmarshall-nested-map-with-jaxb/35321306#35321306
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Removing the @XmlElement annotation for my java.util.Map field

            psonal Pallavi Sonal (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: