当存在静态类时,没有带有@XmlElementDecl的ObjectFactory

时间:2014-06-18 10:11:38

标签: java xml jaxb marshalling jaxb2

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

创建jaxbelement的ObjectFactory类

    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);
    }

  }

类package-info.java,其中提到了响应xml的名称空间

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

         import javax.xml.bind.annotation.*;

编组对象的演示类

      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);
        }

    }

市场类,这是创建jaxbcontext的主要根类

    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;
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:3)

您需要通过传入JAXBContext类或生成的模型的包名来创建ObjectFactory,以确保处理ObjectFactory类。

如果在namespace注释中指定@XmlElementRef属性,则应该可以正常工作。