【问题标题】:There's no ObjectFactory with an @XmlElementDecl when there is static class当有静态类时,没有带有 @XmlElementDecl 的 ObjectFactory
【发布时间】:2014-08-08 14:10:04
【问题描述】:
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);
        }

    }

Market 类,这是创建 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;
                }
            }
        }
    }

【问题讨论】:

    标签: java xml jaxb marshalling jaxb2


    【解决方案1】:

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

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

    【讨论】:

    • 现在我通过了objectfactory,仍然是同样的问题。 JAXBContext jc = JAXBContext.newInstance(ObjectFactory.class,Market.class);
    • 当我传递包名时,得到这个异常,JAXBContext jc = JAXBContext.newInstance("com.jverstry.annotations.generics");线程“主”javax.xml.bind.JAXBException 中的异常:com.jverstry.annotations.generics.Market 类或其任何超类对此上下文都是未知的。在 com.sun.xml.bind.v2.runtime.JAXBContextImpl.getBeanInfo(JAXBContextImpl.java:588)
    • @user3157090 - 您是自己创建模型还是从 XML 模式生成模型?对于包名方法,您可以尝试传入ClassLoader,但最终您会得到与在ObjectFactory 类上创建JAXBContext 相同的结果。
    • 嗨 Blaise,我已经手动创建了模型,你能告诉我如何通过 ClassLoader?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-16
    • 2012-11-16
    • 1970-01-01
    • 1970-01-01
    • 2011-10-25
    • 1970-01-01
    相关资源
    最近更新 更多