JAXB序列化接口到XML问题(Map <string,isomeinterface> not working)</string,isomeinterface>

时间:2012-02-09 20:04:21

标签: java xml jaxb

我正在尝试使用JAXB 2.2.4将接口序列化为XML,但是当我在Map&lt;&gt;中有接口时对象,它爆炸并给我错误:

  

com.sun.xml.bind.v2.runtime.IllegalAnnotationsException:2计数   IllegalAnnotationExceptions com.test.IInterface2是一个接口,和   JAXB无法处理接口。这个问题与此有关   以下位置:at com.test.IInterface2 at public   java.util.Map com.test.Interface1Impl.getI2()at   com.test.Interface1Impl com.test.IInterface2没有no-arg   默认构造函数。这个问题与以下内容有关   location:at public java.util.Map的com.test.IInterface2   com.test.Interface1Impl.getI2()at com.test.Interface1Impl

此代码已经过测试,如果我删除了Map&lt;&gt;,并且如果我使用List&lt;&gt;,甚至可以使用它,那么该代码可以正常工作,但是有一些关于Map&lt;&gt; JAXB不喜欢。

以下是我正在运行的代码,如果您知道解决此问题的方法,请与我们联系!

 package com.test;
    import java.io.StringWriter;
    import javax.xml.bind.JAXBContext;
    import javax.xml.bind.JAXBException;
    import javax.xml.bind.Marshaller;
    import javax.xml.bind.annotation.XmlSeeAlso;

    @XmlSeeAlso({Interface2Impl.class})
    public class main
    {

        /**
         * @param args
         */

        public static void main(String[] args) {

            IInterface1 i1 = new Interface1Impl();
            i1.setA("SET A VALUE");
            i1.setB("Set B VALUE");
            IInterface2 i2 = new Interface2Impl();
            i2.setC("X");
            i2.setD("Y");
            i1.getI2().put("SOMVAL",i2);

            String retval = null;
            try {
                StringWriter writer = new StringWriter();
                JAXBContext context = JAXBContext.newInstance(Interface1Impl.class, Interface2Impl.class);  
                Marshaller m = context.createMarshaller();
                m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);  
                m.marshal(i1, writer);      
                retval = writer.toString();
            } catch (JAXBException ex) {
                //TODO: Log the error here!
                retval = ex.toString();
            }
            System.out.println(retval);

        }
    }

    package com.test;
    import java.util.Map;
    import javax.xml.bind.annotation.XmlRootElement;
    import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
    import com.sun.xml.bind.AnyTypeAdapter;
    @XmlRootElement
    @XmlJavaTypeAdapter(AnyTypeAdapter.class)
    public interface IInterface1
    {
        Map<String,IInterface2> getI2();
        String getA();
        String getB();
        void setA(String a);
        void setB(String b);
        void setI2(Map<String,IInterface2> i2);
    }

    package com.test;
    import java.util.HashMap;
    import java.util.Map;
    import javax.xml.bind.annotation.XmlRootElement;
    @XmlRootElement
    public class Interface1Impl implements IInterface1
    {
        Map<String,IInterface2> i2 = new HashMap<String,IInterface2>();
        String a;
        String b;
        public Interface1Impl()
        {
        }

        public String getA() {
            return a;
        }
        public void setA(String a) {
            this.a = a;
        }
        public String getB() {
            return b;
        }
        public void setB(String b) {
            this.b = b;
        }

        public Map<String,IInterface2> getI2() {
            return i2;
        }

        public void setI2(Map<String,IInterface2> i2) {
            this.i2 = i2;
        }
    }

    package com.test;

    import javax.xml.bind.annotation.XmlRootElement;
    import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
    import com.sun.xml.bind.AnyTypeAdapter;
    @XmlRootElement
    @XmlJavaTypeAdapter(AnyTypeAdapter.class)
    public interface IInterface2
    {
        String getC();
        String getD();

        void setC(String c);
        void setD(String d);
    }

    package com.test;
    import javax.xml.bind.annotation.XmlRootElement;
    @XmlRootElement
    public class Interface2Impl implements IInterface2
    {
        String c;
        String d;

        public Interface2Impl()
        {
        }

        public String getC() {
            return c;
        }
        public void setC(String c) {
            this.c = c;
        }
        public String getD() {
            return d;
        }
        public void setD(String d) {
            this.d = d;
        }
    }

1 个答案:

答案 0 :(得分:5)

要获得以下输出,您可以执行以下操作(请参阅下文):

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<interface1Impl>
    <a>SET A VALUE</a>
    <b>Set B VALUE</b>
    <i2>
        <entry>
            <key>SOMVAL</key>
            <value>
                <c>X</c>
                <d>Y</d>
            </value>
        </entry>
    </i2>
</interface1Impl>

<强> I2Adapter

我们将使用XmlAdapter来处理Map<String, IInterface2>XmlAdapter是一种JAXB机制,它将JAXB无法映射的对象转换为可以映射的对象。

package com.test;

import java.util.*;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.adapters.XmlAdapter;

public class I2Adapter extends XmlAdapter<I2Adapter.AdaptedI2, Map<String, IInterface2>> {

    @Override
    public AdaptedI2 marshal(Map<String, IInterface2> v) throws Exception {
        if(null == v) {
            return null;
        }
        AdaptedI2 adaptedI2 = new AdaptedI2();
        for(Map.Entry<String,IInterface2> entry : v.entrySet()) {
            adaptedI2.entry.add(new Entry(entry.getKey(), entry.getValue()));
        }
        return adaptedI2;
    }

    @Override
    public Map<String, IInterface2> unmarshal(AdaptedI2 v) throws Exception {
        if(null == v) {
            return null;
        }
        Map<String, IInterface2> map = new HashMap<String, IInterface2>();
        for(Entry entry : v.entry) {
            map.put(entry.key, entry.value);
        }
        return map;
    }

    public static class AdaptedI2 {
        public List<Entry> entry = new ArrayList<Entry>();
    }

    public static class Entry {
        public Entry() {
        }

        public Entry(String key, IInterface2 value) {
            this.key = key;
            this.value = value;
        }

        public String key;

        @XmlElement(type=Interface2Impl.class)
        public IInterface2 value;
    }

}

<强> Interface1Impl

@XmlJavaTypeAdapter注释用于注册XmlAdapter。在此示例中,我们将在i2属性上注册它。

package com.test;

import java.util.*;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@XmlRootElement
public class Interface1Impl implements IInterface1 {
    Map<String, IInterface2> i2 = new HashMap<String, IInterface2>();
    String a;
    String b;

    public Interface1Impl() {
    }

    public String getA() {
        return a;
    }

    public void setA(String a) {
        this.a = a;
    }

    public String getB() {
        return b;
    }

    public void setB(String b) {
        this.b = b;
    }

    @XmlJavaTypeAdapter(I2Adapter.class)
    public Map<String, IInterface2> getI2() {
        return i2;
    }

    public void setI2(Map<String, IInterface2> i2) {
        this.i2 = i2;
    }
}

了解更多信息


以下是从非模型类中删除JAXB注释的模型的其余部分:

主要

package com.test;

import java.io.StringWriter;
import javax.xml.bind.*;

public class main {

    /**
     * @param args
     */

    public static void main(String[] args) {

        IInterface1 i1 = new Interface1Impl();
        i1.setA("SET A VALUE");
        i1.setB("Set B VALUE");
        IInterface2 i2 = new Interface2Impl();
        i2.setC("X");
        i2.setD("Y");
        i1.getI2().put("SOMVAL", i2);

        String retval = null;
        try {
            StringWriter writer = new StringWriter();
            JAXBContext context = JAXBContext.newInstance(Interface1Impl.class,
                    Interface2Impl.class);
            Marshaller m = context.createMarshaller();
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            m.marshal(i1, writer);
            retval = writer.toString();
        } catch (JAXBException ex) {
            // TODO: Log the error here!
            retval = ex.toString();
        }
        System.out.println(retval);

    }
}

<强> IInterface1

package com.test;

import java.util.Map;

public interface IInterface1 {
    Map<String, IInterface2> getI2();

    String getA();

    String getB();

    void setA(String a);

    void setB(String b);

    void setI2(Map<String, IInterface2> i2);
}

<强> IInterface2

package com.test;

public interface IInterface2 {
    String getC();

    String getD();

    void setC(String c);

    void setD(String d);
}

<强> Interface2Impl

package com.test;

public class Interface2Impl implements IInterface2 {
    String c;
    String d;

    public Interface2Impl() {
    }

    public String getC() {
        return c;
    }

    public void setC(String c) {
        this.c = c;
    }

    public String getD() {
        return d;
    }

    public void setD(String d) {
        this.d = d;
    }
}