Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

There's no ObjectFactory with an @XmlElementDecl when there is static class

I am getting below exception, i need some help to resolve the issue.


If remove the namespace in the object factory and with out package-info.java class it is working fine.



           Exception that is throwing now                 


        Exception in thread "main" com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
        There's no ObjectFactory with an @XmlElementDecl for the element {}shipping.
        this problem is related to the following location:
        at protected javax.xml.bind.JAXBElement com.jverstry.annotations.generics.Market$Detail.shipping
        at com.jverstry.annotations.generics.Market$Detail
        at protected com.jverstry.annotations.generics.Market$Detail com.jverstry.annotations.generics.Market.detail
        at com.jverstry.annotations.generics.Market

ObjectFactory class which is creating the jaxbelement

    package com.jverstry.annotations.generics;
    import javax.xml.bind.JAXBElement;
    import javax.xml.bind.annotation.XmlElementDecl;
    import javax.xml.bind.annotation.XmlRegistry;
    import javax.xml.namespace.QName;

    import org.example.customer.Customer;

    @XmlRegistry
    public class ObjectFactory {


        public ObjectFactory() {
        }

       public Market.Detail.Shipping createShipping() {
            return new Market.Detail.Shipping();
        }

        private final static QName _Shipping_QNAME = new QName("http://www.example.org/customer", "shipping");

    @XmlElementDecl(namespace = "http://www.example.org/customer", name = "shipping")
    public JAXBElement<Market.Detail.Shipping> createShipping(Market.Detail.Shipping value) {
        return new JAXBElement<Market.Detail.Shipping>(_Shipping_QNAME, Market.Detail.Shipping.class, value);
    }

  }

Class package-info.java, where the name spaces are mentioned for the response xml

          @XmlSchema(namespace = "http://www.example.org/customer", elementFormDefault = XmlNsForm.QUALIFIED)
         package com.jverstry.annotations.generics;

         import javax.xml.bind.annotation.*;

Demo class where marshalling object

      package com.jverstry.annotations.generics;

      import javax.xml.bind.JAXBContext;
      import javax.xml.bind.JAXBElement;
      import javax.xml.bind.Marshaller;

         public class Demo {

          public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(Market.class);

            Market market = new Market();  
            Market.Detail md = new Market.Detail();

            Market.Detail.Shipping mds = new  Market.Detail.Shipping();
            mds.setAvailable(false);

            JAXBElement<Market.Detail.Shipping> shipping = new ObjectFactory().createShipping(mds);
            shipping.setNil(true); 
            md.setShipping(shipping);
            market.setDetail(md);

            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(market, System.out);
        }

    }

Market class, this is the main root class where jaxbcontext is created

    package com.jverstry.annotations.generics;

    import java.math.BigDecimal;

    import javax.xml.bind.JAXBElement;
    import javax.xml.bind.annotation.*;

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = { "detail" })
    @XmlRootElement(name = "Market")
    public class Market
    {

        @XmlElement(required = false)
        protected Market.Detail detail;

        public Market.Detail getDetail() {
            return detail;
        }

        public void setDetail(Market.Detail detail) {
            this.detail = detail;
        }

        @XmlAccessorType(XmlAccessType.FIELD)
        @XmlType(name = "", propOrder = { "shipping" })
        public static class Detail
        {
            @XmlElementRef(name = "shipping")
            protected JAXBElement<Market.Detail.Shipping> shipping;

            public JAXBElement<Market.Detail.Shipping> getShipping() {
                return shipping;
            }

            public void setShipping(JAXBElement<Market.Detail.Shipping> value) {
                this.shipping = value;
            }

            @XmlAccessorType(XmlAccessType.FIELD)
            @XmlType(name = "", propOrder = { "value" })
            public static class Shipping
            {
                @XmlValue
                protected BigDecimal value;

                @XmlAttribute(name = "available")
                protected Boolean available;

                public BigDecimal getValue() {
                    return value;
                }

                public void setValue(BigDecimal value) {
                    this.value = value;
                }

                public Boolean getAvailable() {
                    return available;
                }

                public void setAvailable(Boolean value) {
                    this.available = value;
                }
            }
        }
    }
like image 810
user3157090 Avatar asked Dec 06 '25 23:12

user3157090


1 Answers

You need to create the JAXBContext by passing in the ObjectFactory class or the package name of the generated model to ensure the ObjectFactory class is processed.

If you specify the namespace property on the @XmlElementRef annotation things should work.

like image 136
bdoughan Avatar answered Dec 09 '25 11:12

bdoughan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!