Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
解组时,目标Java对象中字段的MOXy顺序很重要_Java_Jaxb_Moxy_Xmlanyelement - Fatal编程技术网

解组时,目标Java对象中字段的MOXy顺序很重要

解组时,目标Java对象中字段的MOXy顺序很重要,java,jaxb,moxy,xmlanyelement,Java,Jaxb,Moxy,Xmlanyelement,似乎莫西有一只虫子。当类请求中的字段先声明为metaInfo,然后声明为content时,下面的代码段可以完美地工作,但当字段以相反的顺序声明时(content优先,metaInfo其次),测试在解组时失败 引发的异常是: 使用类型:APPLICATION\uxml 原始请求={content=Payload={[one,two,three]},metaInfo=requestMetaInfo={confirmation=false} 封送为application/xml:onetwotherf

似乎莫西有一只虫子。当类请求中的字段先声明为metaInfo,然后声明为content时,下面的代码段可以完美地工作,但当字段以相反的顺序声明时(content优先,metaInfo其次),测试在解组时失败

引发的异常是:
使用类型:APPLICATION\uxml
原始请求={content=Payload={[one,two,three]},metaInfo=requestMetaInfo={confirmation=false}
封送为application/xml:onetwotherfalse
本地异常堆栈:
异常[EclipseLink-32](Eclipse持久性服务-2.6.0.v20150309-bf26070):org.Eclipse.Persistence.exceptions.DescriptorException
异常描述:尝试在对象中为[java.util.collection]类型的实例变量[collection]设置值[[one,two,three]]。指定的对象不是声明基础字段的类或接口的实例,或者展开转换失败。
内部异常:java.lang.IllegalArgumentException:无法将java.util.Collection字段test2.TestCase2$Payload.Collection设置为test2.TestCase2$RequestMetaInfo
映射:org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping[collection]
描述符:XMLDescriptor(test2.TestCase2$Payload-->[数据库表(集合)])
位于org.eclipse.persistence.exceptions.DescriptorException.illegalArgumentWhileSettingValueThruInstanceVariableAccessor(DescriptorException.java:703)
位于org.eclipse.persistence.internal.descriptors.InstanceVariableAttributeAccessor.setAttributeValueInObject(InstanceVariableAttributeAccessor.java:188)
位于org.eclipse.persistence.mappings.DatabaseMapping.setAttributeValueInObject(DatabaseMapping.java:1652)
位于org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping.setAttributeValueInObject(XMLCompositeCollectionMapping.java:741)
位于org.eclipse.persistence.internal.oxm.XMLCompositeCollectionMappingNodeValue.setContainerInstance(XMLCompositeCollectionMappingNodeValue.java:265)
位于org.eclipse.persistence.internal.oxm.record.UnmarshalRecordImpl.endDocument(UnmarshalRecordImpl.java:628)
位于com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endDocument(AbstractSAXParser.java:745)
位于com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:515)
位于com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)
位于com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
位于com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
位于com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
位于com.sun.org.apache.xerces.internal.jaxp.saxpasserimpl$jaxpsaxpasser.parse(saxpasserimpl.java:648)
位于org.eclipse.persistence.internal.oxm.record.XMLReader.parse(XMLReader.java:243)
位于org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:978)
位于org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:425)
位于org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:635)
位于org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:706)
位于org.eclipse.persistence.internal.oxm.xmlumarshaller.unmarshal(xmlumarshaller.java:643)
位于org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:339)
位于test2.TestCase2.main(TestCase2.java:67)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)中
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)中
位于java.lang.reflect.Method.invoke(Method.java:606)
位于com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
原因:java.lang.IllegalArgumentException:无法将java.util.Collection字段test2.TestCase2$Payload.Collection设置为test2.TestCase2$RequestMetaInfo
在sun.reflect.UnsafeFieldAccessorImpl.throwsetilegalargumentexception(UnsafeFieldAccessorImpl.java:164)处
在sun.reflect.UnsafeFieldAccessorImpl.throwsetilegalargumentexception(UnsafeFieldAccessorImpl.java:168)中
在sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:55)
位于sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:75)
位于java.lang.reflect.Field.set(Field.java:741)
位于org.eclipse.persistence.internal.descriptors.InstanceVariableAttributeAccessor.setAttributeValueInObject(InstanceVariableAttributeAccessor.java:141)
... 还有24个
下面是一个重现问题的测试

package test2;

import org.eclipse.persistence.jaxb.JAXBContext;
import org.eclipse.persistence.jaxb.JAXBContextFactory;
import org.eclipse.persistence.jaxb.MarshallerProperties;
import org.eclipse.persistence.jaxb.UnmarshallerProperties;
import org.eclipse.persistence.oxm.MediaType;

import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.*;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;

/**
 * Test that fails if Request.content field declared before than Request.metaInfo, but works well if
 * Request.metaInfo goes first in declaration
 *
 * MOXy 2.6.0
 */
public class TestCase2 {
    @XmlRootElement(name = "request")
    @XmlAccessorType(XmlAccessType.FIELD)
    public static class Request<T> {
        @XmlAnyElement(lax = true)
        private T content; // NB!: Causes test failure if declared before metaInfo, if declaration order is changed, things work

        @XmlElement
        private RequestMetaInfo metaInfo;

        public Request() {
        }

        public Request(T content, RequestMetaInfo metaInfo) {
            this.content = content;
            this.metaInfo = metaInfo;
        }

        @Override
        public String toString() {
            return "request = {" + "content=" + content + ", metaInfo=" + metaInfo +'}';
        }

    }
    @XmlRootElement
    @XmlAccessorType(XmlAccessType.FIELD)
    public static class RequestMetaInfo {
        @XmlElement
        private Boolean confirmation = false;
        @Override
        public String toString() {
            return "requestMetaInfo = {" + "confirmation=" + confirmation +'}';
        }
    }

    @XmlRootElement(name = "collection")
    @XmlAccessorType(XmlAccessType.FIELD)
    public static class Payload<T> {
        @XmlElement
        private Collection collection = new ArrayList();

        public Payload(){}
        public Payload(Collection<T> collection){
            this.collection.addAll(collection);
        }

        public Collection<T> getCollection() {
            return collection;
        }

        @Override
        public String toString() {
            return "Payload = {" + getCollection()+"}";
        }
    }

    // Element name holding value of primitive types
    public static final String VALUE_ELEMENT = "value";
    // Attribute prefix in JSON
    public static final String ATTRIBUTE_PREFIX = "@";
    public static void main(String... args) {
        try {
            for (MediaType type : new MediaType[]{MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) {
                System.out.println("Going with type: " + type);
                JAXBContext context = (JAXBContext) JAXBContextFactory.createContext(
                    new Class[]{
                        Request.class,
                        RequestMetaInfo.class,
                        Payload.class
                    },
                    Collections.emptyMap());

                Marshaller marshaller = context.createMarshaller();
                marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, type);
                marshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
                marshaller.setProperty(MarshallerProperties.JSON_ATTRIBUTE_PREFIX, ATTRIBUTE_PREFIX);
                marshaller.setProperty(MarshallerProperties.JSON_VALUE_WRAPPER, VALUE_ELEMENT);
                marshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);

                Request original = new Request(
                    new Payload(Arrays.asList("one","two","three")),
                    new RequestMetaInfo()
                );
                System.out.println("Original " + original.toString());

                StreamResult result = new StreamResult(new StringWriter());
                marshaller.marshal(original, result);
                String generated = result.getWriter().toString();
                System.out.println("Marshaled as " + type.getMediaType() + ": " + generated);

                Unmarshaller unmarshaller = context.createUnmarshaller();
                unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, type);
                unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
                unmarshaller.setProperty(UnmarshallerProperties.JSON_ATTRIBUTE_PREFIX, ATTRIBUTE_PREFIX);
                unmarshaller.setProperty(UnmarshallerProperties.JSON_VALUE_WRAPPER, VALUE_ELEMENT);
                unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);


                Request unmarshalled = unmarshaller.unmarshal(new StreamSource(new StringReader(generated)), Request.class).getValue();
                System.out.println("Unmarshaled " + unmarshalled.toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
包测试2;
导入org.eclipse.persistence.jaxb.JAXBContext;
导入org.eclipse.persistence.jaxb.JAXBContextFactory;
导入org.eclipse.persistence.jaxb.MarshallerProperties;
导入org.eclipse.persistence.jaxb.UnmarshallerProperties;
导入org.eclipse.persistence.oxm.MediaType;
导入javax.xml.bind.Marshaller;
导入javax.xml.bind.Unmarshaller;
导入javax.xml.bind.annotation.*;
导入javax.xml.transform.stream.StreamResult;
导入javax.xml.transform.stream.StreamSource;
导入java.io.StringReader;
导入java.io.StringWriter;
导入java.util.ArrayList;
导入java.util.array;
导入java.util.Collection;
导入java.util.Collections;
/**
*如果Request.content字段在Request.metaInfo之前声明,则测试失败,但如果
*在声明中,Request.metaInfo放在第一位
*
*MOXy 2.6.0
*/
公共类测试用例2{
@XmlRootElement(name=“request”)
@XmlAccessorType(XmlAccessType.FIELD)
公共静态类请求{
@xmlanyement(lax=true)
private T content;//NB!:如果在metaInfo之前声明,则会导致测试失败,如果声明顺序更改,则一切正常
package test2;

import org.eclipse.persistence.jaxb.JAXBContext;
import org.eclipse.persistence.jaxb.JAXBContextFactory;
import org.eclipse.persistence.jaxb.MarshallerProperties;
import org.eclipse.persistence.jaxb.UnmarshallerProperties;
import org.eclipse.persistence.oxm.MediaType;

import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.*;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;

/**
 * Test that fails if Request.content field declared before than Request.metaInfo, but works well if
 * Request.metaInfo goes first in declaration
 *
 * MOXy 2.6.0
 */
public class TestCase2 {
    @XmlRootElement(name = "request")
    @XmlAccessorType(XmlAccessType.FIELD)
    public static class Request<T> {
        @XmlAnyElement(lax = true)
        private T content; // NB!: Causes test failure if declared before metaInfo, if declaration order is changed, things work

        @XmlElement
        private RequestMetaInfo metaInfo;

        public Request() {
        }

        public Request(T content, RequestMetaInfo metaInfo) {
            this.content = content;
            this.metaInfo = metaInfo;
        }

        @Override
        public String toString() {
            return "request = {" + "content=" + content + ", metaInfo=" + metaInfo +'}';
        }

    }
    @XmlRootElement
    @XmlAccessorType(XmlAccessType.FIELD)
    public static class RequestMetaInfo {
        @XmlElement
        private Boolean confirmation = false;
        @Override
        public String toString() {
            return "requestMetaInfo = {" + "confirmation=" + confirmation +'}';
        }
    }

    @XmlRootElement(name = "collection")
    @XmlAccessorType(XmlAccessType.FIELD)
    public static class Payload<T> {
        @XmlElement
        private Collection collection = new ArrayList();

        public Payload(){}
        public Payload(Collection<T> collection){
            this.collection.addAll(collection);
        }

        public Collection<T> getCollection() {
            return collection;
        }

        @Override
        public String toString() {
            return "Payload = {" + getCollection()+"}";
        }
    }

    // Element name holding value of primitive types
    public static final String VALUE_ELEMENT = "value";
    // Attribute prefix in JSON
    public static final String ATTRIBUTE_PREFIX = "@";
    public static void main(String... args) {
        try {
            for (MediaType type : new MediaType[]{MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) {
                System.out.println("Going with type: " + type);
                JAXBContext context = (JAXBContext) JAXBContextFactory.createContext(
                    new Class[]{
                        Request.class,
                        RequestMetaInfo.class,
                        Payload.class
                    },
                    Collections.emptyMap());

                Marshaller marshaller = context.createMarshaller();
                marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, type);
                marshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
                marshaller.setProperty(MarshallerProperties.JSON_ATTRIBUTE_PREFIX, ATTRIBUTE_PREFIX);
                marshaller.setProperty(MarshallerProperties.JSON_VALUE_WRAPPER, VALUE_ELEMENT);
                marshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);

                Request original = new Request(
                    new Payload(Arrays.asList("one","two","three")),
                    new RequestMetaInfo()
                );
                System.out.println("Original " + original.toString());

                StreamResult result = new StreamResult(new StringWriter());
                marshaller.marshal(original, result);
                String generated = result.getWriter().toString();
                System.out.println("Marshaled as " + type.getMediaType() + ": " + generated);

                Unmarshaller unmarshaller = context.createUnmarshaller();
                unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, type);
                unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
                unmarshaller.setProperty(UnmarshallerProperties.JSON_ATTRIBUTE_PREFIX, ATTRIBUTE_PREFIX);
                unmarshaller.setProperty(UnmarshallerProperties.JSON_VALUE_WRAPPER, VALUE_ELEMENT);
                unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);


                Request unmarshalled = unmarshaller.unmarshal(new StreamSource(new StringReader(generated)), Request.class).getValue();
                System.out.println("Unmarshaled " + unmarshalled.toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
@XmlMixed
@XmlAnyElement(lax = true)
private String content;