java-当有静态类时,没有带有@XmlElementDecl的ObjectFactory

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

解决方法:

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

如果在@XmlElementRef注释上指定了namespace属性,则应该可以使用.

上一篇:是否保证此C#结构具有所需的大小?


下一篇:java-将JAXB编组为复杂类型的布尔值