JAXB使用ID引用而不是包含来序列化XML

时间:2012-11-21 18:22:40

标签: java jaxb xml-serialization jersey

在RestFul-Webservice(Jersey)上下文中,我需要将Object图形编组/序列化为XML和JSON。为简单起见,我尝试用2-3个类来解释这个问题:

Person.java

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Person {

    private String name;

    // @XmlIDREF
    @XmlElement(name = "house")
    @XmlElementWrapper(name = "houses")
    private Collection<House> houses;

    public Person() {}

    public Person(String name, Collection<House> houses) {
        this.name = name;
        this.houses = houses;
    }
}

House.java

@XmlAccessorType(XmlAccessType.FIELD)
public class House {

    // @XmlID
    public String name;

    public String location;

    public House() {}

    public House(String name, String location) {
        this.name = name;
        this.location = location;
    }
}

现在,当我序列化一个Person时,XML将如下所示:

<people>
    <person>
        <name>Edward</name>
        <houses>
            <house>
                <name>MyAppartment</name>
                <location>London</location>
            </house>
            <house>
                <name>MySecondAppartment</name>
                <location>London</location>
            </house>
        </houses>
    </person>

    <person>
        <name>Thomas</name>
        <houses>
            <house>
                <name>MyAppartment</name>
                <location>London</location>
            </house>
            <house>
                <name>MySecondAppartment</name>
                <location>London</location>
            </house>
        </houses>
    </person>
</people>

这里的问题是,多次列出相同的房屋。现在我添加了未注释的XmlIDREFXmlID注释,这将导致XML与此类似:

<people>
    <person>
        <name>Edward</name>
        <houses>
            <house>MyAppartment</house>
            <house>MySecondAppartment</house>
        </houses>
    </person>

    <person>
        <name>Thomas</name>
        <houses>
            <house>MyAppartment</house>
            <house>MySecondAppartment</house>
        </houses>
    </person>
</people>

虽然第一个XML太冗长,但这个缺乏信息。我如何创建(和解组)类似于:

的东西
<people>
    <person>
        <name>Edward</name>
        <houses>
            <house>MyAppartment</house>
            <house>MySecondAppartment</house>
        </houses>
    </person>

    <person>
        <name>Thomas</name>
        <houses>
            <house>MyAppartment</house>
            <house>MySecondAppartment</house>
        </houses>
    </person>

    <houses>
        <house>
            <name>MyAppartment</name>
            <location>London</location>
        </house>
        <house>
            <name>MySecondAppartment</name>
            <location>London</location>
        </house>
    </houses>
</people>

解决方案应该是通用的,因为我不想为对象图中的每个新元素编写额外的类。

为了完整起见,这是宁静的网络服务:

@Path("rest/persons")
public class TestService {
    @GET
    @Produces({ MediaType.TEXT_XML, MediaType.APPLICATION_JSON })
    public Collection<Person> test() throws Exception {
        Collection<Person> persons = new ArrayList<Person>();
        Collection<House> houses = new HashSet<House>();
        houses.add(new House("MyAppartment", "London"));
        houses.add(new House("MySecondAppartment", "London"));
        persons.add(new Person("Thomas", houses));
        persons.add(new Person("Edward", houses));
        return persons;
    }
}

提前致谢。

1 个答案:

答案 0 :(得分:2)

如果您尝试序列化为与您给出的最后一个XML示例匹配的格式,那么我相信您的对象图形结构不正确无法实现。

如果要提供Person个对象及其相关房屋的集合,并提供House个对象的集合,则需要返回包含两个集合的序列化XML消息。看起来好像您在正确的地方有@XmlIDREF@XmlID注释,可以根据您的描述制作Person-House关联,但您只返回{{1对象而不是返回两个集合。

您的网络服务应该看起来更像这样(省略序列化,因为您似乎清楚如何序列化它):

Person

还有其他方法可以实现这一目标(例如,创建一个包含人和房屋集合属性的包装器对象等),但希望你能得到这个想法。