6

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

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

房子.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;
    }
}

提前致谢。

4

1 回答 1

2

如果您尝试序列化为与您提供的最后一个 XML 示例匹配的格式,那么我相信您的对象图的结构不正确以实现这一目标。

如果您想提供一个Person对象集合及其关联的房屋,并提供一个House对象集合,那么您需要返回一个包含这两个集合的序列化 XML 消息。看起来好像您在正确的位置有您的@XmlIDREF@XmlID注释,以便按照您的意图(根据您的描述)建立人屋关联,但您只返回Person对象的集合,而不是返回两个集合。

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

@Path("rest/persons")
public class TestService {
    @GET
    @Produces({ MediaType.TEXT_XML, MediaType.APPLICATION_JSON })
    public Map<String, Object> test() throws Exception {
        Map<String, Object> peopleAndHouses = new HashMap<String, Object>();
        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));

        peopleAndHouses.put("houses", houses);
        peopleAndHouses.put("people", persons);
        return peopleAndHouses;
    }
}

还有其他方法可以实现这一点(例如,创建一个具有人和房屋的集合属性的包装器对象等),但希望您能明白这一点。

于 2013-01-08T13:25:38.947 回答