4

在实现我的项目的 Web 服务层时,我正在处理一些循环引用。我正在使用 jaxb(最新版本,2.2.7),甚至我也看过一些提示,因为这里这里我无法让它工作。这是关于我的问题的基本SSCCE :

/*
* The service interface
*/
@WebService
public interface IMyWS {

    @WebMethod
    public List<Class1> cyclicTest();

}

/*
* Interface implementation
*/
@WebService(endpointInterface = "com.mycompany.ws.interfaces.IMyWS")
public class MyWS implements IMyWS {

    @XmlRootElement
    public static class Class1 {

        @XmlTransient
        private Class2 class2;

        public Class1() {

        }

        public Class1(Class2 refClass) {
            class2 = refClass;
        }

        public Class2 getClass2() {
            return class2;
        }

        public void setClass2(Class2 class2) {
            this.class2 = class2;
        }

        @Override
        public String toString() {
            return this.getClass().getSimpleName();
        }

    }

    @XmlRootElement
    public static class Class2 {

        private Class1 class1;

        public Class2() {

        }

        public Class1 getClass1() {
            return class1;
        }

        public void setClass1(Class1 class1) {
            this.class1 = class1;
        }

        @Override
        public String toString() {
            return this.getClass().getSimpleName();
        }
    }

    @Override
    public List<Class1> cyclicTest() {
        //I create an instance of each class, having them a cyclic reference to the other instance
        Class2 class2 = new Class2();
        Class1 class1 = new Class1(class2);
        class2.setClass1(class1);
        return Arrays.asList(class1);
    }

}

我在调用时实际上正在处理的异常cyclicTest()

Caused by: javax.xml.bind.MarshalException
 - with linked exception:
[com.sun.istack.SAXException2: Se ha detectado un ciclo en el gráfico de objeto. Esto provocará un XML con profundidad infinita: Class1 -> Class2 -> Class1]
    at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:326)
    at com.sun.xml.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:178)
    at org.apache.cxf.jaxb.JAXBEncoderDecoder.writeObject(JAXBEncoderDecoder.java:537)
    at org.apache.cxf.jaxb.JAXBEncoderDecoder.marshall(JAXBEncoderDecoder.java:233)
    ... 50 more
Caused by: com.sun.istack.SAXException2: Se ha detectado un ciclo en el gráfico de objeto. Esto provocará un XML con profundidad infinita: Class1 -> Class2 -> Class1
    at com.sun.xml.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:249)
    at com.sun.xml.bind.v2.runtime.XMLSerializer.pushObject(XMLSerializer.java:537)
    at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:631)
    at com.sun.xml.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody(SingleElementNodeProperty.java:158)
    at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:361)
    at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:696)
    at com.sun.xml.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody(SingleElementNodeProperty.java:158)
    at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:361)
    at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:696)
    at com.sun.xml.bind.v2.runtime.property.ArrayElementNodeProperty.serializeItem(ArrayElementNodeProperty.java:69)
    at com.sun.xml.bind.v2.runtime.property.ArrayElementProperty.serializeListBody(ArrayElementProperty.java:172)
    at com.sun.xml.bind.v2.runtime.property.ArrayERProperty.serializeBody(ArrayERProperty.java:159)
    at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:361)
    at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:696)
    at com.sun.xml.bind.v2.runtime.ElementBeanInfoImpl$1.serializeBody(ElementBeanInfoImpl.java:156)
    at com.sun.xml.bind.v2.runtime.ElementBeanInfoImpl$1.serializeBody(ElementBeanInfoImpl.java:131)
    at com.sun.xml.bind.v2.runtime.ElementBeanInfoImpl.serializeBody(ElementBeanInfoImpl.java:333)
    at com.sun.xml.bind.v2.runtime.ElementBeanInfoImpl.serializeRoot(ElementBeanInfoImpl.java:340)
    at com.sun.xml.bind.v2.runtime.ElementBeanInfoImpl.serializeRoot(ElementBeanInfoImpl.java:76)
    at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:494)
    at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:323)
    ... 53 more

我想我设置了正确的注释。我实际上错过了什么?

4

3 回答 3

5

使用 @XmlID 和 @XmlIDREF 应该可以解决您的问题。

这是我所做的:

我在 Class 1 中添加了一个 String 字段用作 id,并用 @XmlID. 然后,我setClass1()@XmlIDREF. 我的测试在下面的主要方法中。

/*
 * Interface implementation
 */
@WebService(endpointInterface = "com.mycompany.ws.interfaces.IMyWS")
public class MyWS implements IMyWS {

    @XmlRootElement
    public static class Class1 {

        @XmlID
        private String id;

        private Class2 class2;

        public Class1() {
            this.id = UUID.randomUUID().toString();
        }

        public Class1(Class2 refClass) {
            this();
            class2 = refClass;
        }

        public Class2 getClass2() {
            return class2;
        }

        public void setClass2(Class2 class2) {
            this.class2 = class2;
        }

        @Override
        public String toString() {
            return this.getClass().getSimpleName();
        }

    }

    @XmlRootElement
    public static class Class2 {

        private Class1 class1;

        public Class2() {

        }

        public Class1 getClass1() {
            return class1;
        }

        @XmlIDREF
        public void setClass1(Class1 class1) {
            this.class1 = class1;
        }

        @Override
        public String toString() {
            return this.getClass().getSimpleName();
        }
    }

    @Override
    public List<Class1> cyclicTest() {
        //I create an instance of each class, having them a cyclic reference to the other instance
        Class2 class2 = new Class2();
        Class1 class1 = new Class1(class2);
        class2.setClass1(class1);
        return Arrays.asList(class1);
    }

    public static void main(String[] args) throws JAXBException {

        JAXBContext ctx = JAXBContext.newInstance(Class1.class);
        Marshaller m = ctx.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        List<Class1> class1s = new MyWS().cyclicTest();

        for (Class1 c1 : class1s){
            m.marshal(c1, System.out);
        }

    }

}

输出:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<class1>
    <id>e9e53e73-9a96-4c7c-b919-3ed3d7aa4c5e</id>
    <class2>
        <class1>e9e53e73-9a96-4c7c-b919-3ed3d7aa4c5e</class1>
    </class2>
</class1>
于 2013-09-01T17:39:01.110 回答
3

当您添加 @XmlTransient私有财产时,您应该将XmlAccessType(默认为XmlAccessType.PUBLIC_MEMBER)更改为XmlAccessType.PROPERTY

PUBLIC_MEMBER是 JAXB 中的默认访问类型。这意味着 JAXB 实现将为以下内容生成绑定:公共字段、注释字段、属性

从 WA 复制的代码:

@WebService(endpointInterface = "com.mycompany.ws.interfaces.IMyWS")
public class MyWS implements IMyWS {

@XmlRootElement
@XmlAccessorType(XmlAccessType.PROPERTY)
public static class Class1 {

    private Class2 class2;

    public Class1() {

    }

    public Class1(Class2 refClass) {
        class2 = refClass;
    }

    @XmlTransient
    public Class2 getClass2() {
        return class2;
    }

    public void setClass2(Class2 class2) {
        this.class2 = class2;
    }

    @Override
    public String toString() {
        return this.getClass().getSimpleName();
    }

}

@XmlRootElement
public static class Class2 {

    private Class1 class1;

    public Class2() {

    }

    public Class1 getClass1() {
        return class1;
    }

    public void setClass1(Class1 class1) {
        this.class1 = class1;
    }

    @Override
    public String toString() {
        return this.getClass().getSimpleName();
    }
}

public List<Class1> cyclicTest() {
    //I create an instance of each class, having them a cyclic reference to the other instance
    Class2 class2 = new Class2();
    Class1 class1 = new Class1(class2);
    class2.setClass1(class1);
    return Arrays.asList(class1);
}


public static void main(String[] args) throws JAXBException {

    JAXBContext ctx = JAXBContext.newInstance(Class1.class);
    Marshaller m = ctx.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    List<Class1> class1s = new MyWs().cyclicTest();

    for (Class1 c1 : class1s){
        m.marshal(c1, System.out);
    }

}
于 2013-08-30T10:59:23.057 回答
0

添加到 W Almir 的答案中,为了更简单地为对象生成唯一 ID(无需向构造函数添加代码),请注释一个返回 id 的方法。例如:

@XmlID
public String getId()
{
    return Integer.toString(System.identityHashCode(this));
}

这似乎不仅适用于将 Java 对象的循环图转换为 XML,而且也适用于将 XML 转换为 Java 对象的循环图。

于 2017-11-07T00:36:15.973 回答