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

JAXB doesn't work with java.time.Instant

    XMLWordPrintable

Details

    • Enhancement
    • Resolution: Duplicate
    • P4
    • tbd
    • 8, 9
    • xml
    • x86_64
    • windows_7

    Description

      FULL PRODUCT VERSION :
      java version "1.8.0_05"
      Java(TM) SE Runtime Environment (build 1.8.0_05-b13)
      Java HotSpot(TM) 64-Bit Server VM (build 25.5-b02, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.1.7601]

      A DESCRIPTION OF THE PROBLEM :
      JRE 8 contains both, java.time API and JAXB. When trying to marshal / unmarshal java.time.Instant, JAXB fails due to unsupported data type. As java.time is intended to be a replacement for java.util.Date, and as both APIs are part of the same JRE, developers expect this mismatch to be a bug simply.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Execute the program shown below.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Marshals and unmarshals java.time.Instant using ISO UTC format.
      ACTUAL -
      Fails this exception due to unsupported data type.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      Exception in thread "main" javax.xml.bind.DataBindingException: com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
      @XmlAttribute/@XmlValue muss einen Java-Typ referenzieren, der Text in XML zugeordnet ist.
      this problem is related to the following location:
      at java.time.Instant TimeTest$T.i
      at TimeTest$T

      at javax.xml.bind.JAXB._marshal(Unknown Source)
      at javax.xml.bind.JAXB.marshal(Unknown Source)
      at TimeTest.main(TimeTest.java:12)
      Caused by: com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
      @XmlAttribute/@XmlValue muss einen Java-Typ referenzieren, der Text in XML zugeordnet ist.
      this problem is related to the following location:
      at java.time.Instant TimeTest$T.i
      at TimeTest$T

      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 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      at java.lang.reflect.Method.invoke(Unknown Source)
      at javax.xml.bind.ContextFinder.newInstance(Unknown Source)
      at javax.xml.bind.ContextFinder.newInstance(Unknown Source)
      at javax.xml.bind.ContextFinder.find(Unknown Source)
      at javax.xml.bind.JAXBContext.newInstance(Unknown Source)
      at javax.xml.bind.JAXBContext.newInstance(Unknown Source)
      at javax.xml.bind.JAXB$Cache.<init>(Unknown Source)
      at javax.xml.bind.JAXB.getContext(Unknown Source)
      ... 3 more


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.io.StringReader;
      import java.time.Instant;

      import javax.xml.bind.JAXB;
      import javax.xml.bind.annotation.XmlAttribute;
      import javax.xml.bind.annotation.XmlRootElement;

      public class TimeTest {
          public static void main(String[] a) {
              T t = new T();
              t.i = Instant.now();
              JAXB.marshal(t, System.out);
              System.out.println(JAXB.unmarshal(new StringReader("<T i=\"2014-05-05T11:05:00.000Z\"/>"), T.class).i);
          }

          @XmlRootElement
          public static class T {
              @XmlAttribute
              Instant i;
          }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Use explicit marshalling / unmarshalling code at each place where java.time API is to be used with JAXB.

              @XmlAttribute
              public String getI() {
                  return this.i.toString();
              }

              public void setI(String i) {
                  this.i = Instant.parse(i);
              }


      Attachments

        Issue Links

          Activity

            People

              zli Zheng Jun Li (Inactive)
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: