jaxb
  1. jaxb
  2. JAXB-1012

No default constructor found on class javax.xml.bind.JAXBElement - Need know why

    Details

    • Type: Improvement Improvement
    • Status: Open
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: 2.2.7
    • Fix Version/s: None
    • Component/s: runtime
    • Labels:
      None
    • Environment:

      Win 7 64b, JavaSE 7

      Description

      Hi,

      I have generated Java code from XSD, using xjc tool, sheme is here : http://dd.eionet.europa.eu/schemas/id2011850eu/AirQualityReporting.xsd

      I can marshall XML from it, and its valid against XSD: http://www.valachnet.cz/lvanek/0/CZ_E2a_hourly.xml

      While unmarshall back, I get exception:

      com.sun.xml.bind.v2.ClassFactory create0
      INFO: No default constructor found on class javax.xml.bind.JAXBElement
      java.lang.NoSuchMethodException: javax.xml.bind.JAXBElement.<init>()
      at java.lang.Class.getConstructor0(Unknown Source)
      at java.lang.Class.getDeclaredConstructor(Unknown Source)
      at com.sun.xml.bind.v2.ClassFactory.create0(ClassFactory.java:104)
      at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.createInstance(ClassBeanInfoImpl.java:286)

      I discovered, that JAXB creates in method 'ClassBeanInfoImpl::createInstance'
      jaxbType = javax.xml.bind.JAXBElement - But it's not have default constructor.

      I need to know which XML fragment causes my problem, but NoSuchMethodException isn't informative.
      It is possible surround calling: bean = ClassFactory.create0(jaxbType); in 'ClassBeanInfoImpl::createInstance' method with try/catch and throw more descriptive exception ?

      Bye,
      Lumir Vanek

        Activity

        Hide
        laune added a comment -

        The indicated XML schema loads several other xsd files from the net and does not compile, needing customizations. Please provide the customization as you have used it. Also, the top level element of the XML file gml:FeatureCollection is not defined in the schema file. Also, complete code to reproduce the unmarshalling should be provided.

        Hoewever, it's very likely that this is a case of incomplete customization and/or JAXB use and should not be discussed in this JIRA, where bubs are reported. Please use the user list for further discussion since JIRA is rather inconvenient for this.

        Show
        laune added a comment - The indicated XML schema loads several other xsd files from the net and does not compile, needing customizations. Please provide the customization as you have used it. Also, the top level element of the XML file gml:FeatureCollection is not defined in the schema file. Also, complete code to reproduce the unmarshalling should be provided. Hoewever, it's very likely that this is a case of incomplete customization and/or JAXB use and should not be discussed in this JIRA, where bubs are reported. Please use the user list for further discussion since JIRA is rather inconvenient for this.
        Hide
        vanekl added a comment -

        Hi,
        my customization is:

        http://www.valachnet.cz/lvanek/0/bindings.xjb

        Ant script, that generate Java code is:

        http://www.valachnet.cz/lvanek/0/generate_jaxb_code.xml

        And unmarshalling code:

        import java.io.File;
        import java.net.MalformedURLException;
        import java.net.URL;

        import javax.xml.bind.JAXBContext;
        import javax.xml.bind.JAXBElement;
        import javax.xml.bind.JAXBException;
        import javax.xml.bind.Unmarshaller;
        import javax.xml.bind.ValidationEvent;
        import javax.xml.bind.ValidationEventHandler;
        import javax.xml.transform.stream.StreamSource;
        import javax.xml.validation.Schema;
        import javax.xml.validation.SchemaFactory;
        import javax.xml.validation.Validator;

        import org.xml.sax.SAXException;

        import net.opengis.gml._3.FeatureCollectionType;

        public static void main(String[] args)
        {
        try
        {
        System.out.println("Creating JAXB Context");
        final JAXBContext jaxbContext = JAXBContext.newInstance(FeatureCollectionType.class);

        /*

        • Input XML
          */
          String fileName = ".\\output
          CZ_E2a_hourly.xml";

        /*

        • Loading XSD
          */
          System.out.println("Loading XSD");
          SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
          // Schema schema = schemaFactory.newSchema(new File("..\\AirQualityReportingModel\\XSD
          AirQualityReporting_0.3.7c.xsd"));
          Schema schema = schemaFactory.newSchema(new URL("http://dd.eionet.europa.eu/schemas/id2011850eu/AirQualityReporting.xsd"));

        /*

        • Validation
          */
          System.out.println("Validating " + fileName + "...");

        try

        { Validator validator = schema.newValidator(); validator.validate(new StreamSource(new File(fileName))); System.out.println("OK"); }

        catch (Exception e)

        { e.printStackTrace(); return; }

        /*

        • Unmarsalling
          */
          final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
          unmarshaller.setSchema(schema);

        unmarshaller.setEventHandler(new ValidationEventHandler()
        {
        @Override
        public boolean handleEvent(ValidationEvent event)
        {
        System.out.println(event.getMessage());
        return true;
        }}
        );

        JAXBElement<FeatureCollectionType> featureCollection = unmarshaller.unmarshal(new StreamSource(fileName), FeatureCollectionType.class);
        System.out.println(featureCollection.getName());

        final FeatureCollectionType featureCollectionType = featureCollection.getValue();
        System.out.println(featureCollectionType.getDescription());
        }
        catch (JAXBException | SAXException | MalformedURLException e)

        { e.printStackTrace(); }

        }

        Bye,
        Lumir

        Show
        vanekl added a comment - Hi, my customization is: http://www.valachnet.cz/lvanek/0/bindings.xjb Ant script, that generate Java code is: http://www.valachnet.cz/lvanek/0/generate_jaxb_code.xml And unmarshalling code: import java.io.File; import java.net.MalformedURLException; import java.net.URL; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBElement; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; import javax.xml.bind.ValidationEvent; import javax.xml.bind.ValidationEventHandler; import javax.xml.transform.stream.StreamSource; import javax.xml.validation.Schema; import javax.xml.validation.SchemaFactory; import javax.xml.validation.Validator; import org.xml.sax.SAXException; import net.opengis.gml._3.FeatureCollectionType; public static void main(String[] args) { try { System.out.println("Creating JAXB Context"); final JAXBContext jaxbContext = JAXBContext.newInstance(FeatureCollectionType.class); /* Input XML */ String fileName = ".\\output CZ_E2a_hourly.xml"; /* Loading XSD */ System.out.println("Loading XSD"); SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); // Schema schema = schemaFactory.newSchema(new File("..\\AirQualityReportingModel\\XSD AirQualityReporting_0.3.7c.xsd")); Schema schema = schemaFactory.newSchema(new URL("http://dd.eionet.europa.eu/schemas/id2011850eu/AirQualityReporting.xsd")); /* Validation */ System.out.println("Validating " + fileName + "..."); try { Validator validator = schema.newValidator(); validator.validate(new StreamSource(new File(fileName))); System.out.println("OK"); } catch (Exception e) { e.printStackTrace(); return; } /* Unmarsalling */ final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); unmarshaller.setSchema(schema); unmarshaller.setEventHandler(new ValidationEventHandler() { @Override public boolean handleEvent(ValidationEvent event) { System.out.println(event.getMessage()); return true; }} ); JAXBElement<FeatureCollectionType> featureCollection = unmarshaller.unmarshal(new StreamSource(fileName), FeatureCollectionType.class); System.out.println(featureCollection.getName()); final FeatureCollectionType featureCollectionType = featureCollection.getValue(); System.out.println(featureCollectionType.getDescription()); } catch (JAXBException | SAXException | MalformedURLException e) { e.printStackTrace(); } } Bye, Lumir
        Hide
        laune added a comment -

        Works for me, using data as posted and xjc and java from /extra/JDK7u21/jdk1.7.0_21/. Some other effect has caused validation warnings which I haven't investigated. (I'll not continue this thread via JIRA.)

        Show
        laune added a comment - Works for me, using data as posted and xjc and java from /extra/JDK7u21/jdk1.7.0_21/. Some other effect has caused validation warnings which I haven't investigated. (I'll not continue this thread via JIRA.)
        Hide
        vanekl added a comment - - edited

        Hi,
        after some hours of debugging I found cause of problem:

        Class (generated by jxc): net.opengis.om._2.OMObservationType

        have declared:

        @XmlElement(required = true, type = JAXBElement.class)
        protected Object result;

        After removing "type = JAXBElement.class" Unmashalling works fine.

        I am still belived, that throwing more descriptive exceptions (ideally containing tag name)
        in this case would be welcomed by JAXB users.

        class UnmarshallingContext::createInstance( JaxBeanInfo beanInfo ) may be good place to cath
        NoSuchMethodException and send it to Loader.reportError().

        You can close this issue, thanks for help.

        Best Regards,
        Lumir Vanek

        Show
        vanekl added a comment - - edited Hi, after some hours of debugging I found cause of problem: Class (generated by jxc): net.opengis.om._2.OMObservationType have declared: @XmlElement(required = true, type = JAXBElement.class) protected Object result; After removing "type = JAXBElement.class" Unmashalling works fine. I am still belived, that throwing more descriptive exceptions (ideally containing tag name) in this case would be welcomed by JAXB users. class UnmarshallingContext::createInstance( JaxBeanInfo beanInfo ) may be good place to cath NoSuchMethodException and send it to Loader.reportError(). You can close this issue, thanks for help. Best Regards, Lumir Vanek

          People

          • Assignee:
            Iaroslav Savytskyi
            Reporter:
            vanekl
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated: