我有一个具有双向关系的类的编组问题。 以下示例将通过编组PhoneNumber实例生成无效的xml。
<?xml version="1.0" encoding="UTF-8"?>
<phonenumber>
<person>
<phone-numbers>
<firstname>yvonne</firstname>
</person>
<number>12345</number>
</phonenumber>
编组时不会抛出任何异常。 没有XmlElementWrapper注释,它可以工作。 这是一个错误还是我错过了什么?
编组一个Person实例的另一种方法是使用或不使用 XmlElementWrapper注释。
我使用的是EclispseLink MOXy 2.5.1
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import org.eclipse.persistence.jaxb.JAXBContextFactory;
import org.eclipse.persistence.jaxb.MarshallerProperties;
import org.eclipse.persistence.oxm.MediaType;
import org.eclipse.persistence.oxm.annotations.XmlInverseReference;
public final class XmlElementWrapperApp {
@XmlRootElement(name = "person")
static class PersonEntity {
private String firstname;
@XmlElementWrapper(name = "phone-numbers")
@XmlElement(name = "phone")
@XmlInverseReference(mappedBy = "person")
private List<PhoneNumberEntity> phoneNumbers;
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getFirstname() {
return firstname;
}
public void setPhoneNumbers(List<PhoneNumberEntity> phoneNumbers) {
this.phoneNumbers = new ArrayList<>(phoneNumbers);
}
public List<PhoneNumberEntity> getPhoneNumbers() {
return phoneNumbers;
}
public void addPhoneNumber(PhoneNumberEntity phoneNumber) {
if (phoneNumbers == null) {
phoneNumbers = new ArrayList<>();
}
this.phoneNumbers.add(phoneNumber);
}
}
@XmlRootElement(name = "phonenumber")
static class PhoneNumberEntity {
private String number;
@XmlElement
@XmlInverseReference(mappedBy = "phoneNumbers")
private PersonEntity person;
public void setNumber(String number) {
this.number = number;
}
public String getNumber() {
return number;
}
void setPerson(PersonEntity person) {
this.person = person;
}
PersonEntity getPerson() {
return person;
}
}
public static void main(String[] args) throws JAXBException {
testPerson();
testPhoneNumber();
}
static void testPerson() throws JAXBException {
System.out.println("person:");
PersonEntity personEntity = createPersonEntity();
String entityJson = marshal(personEntity);
System.out.println(entityJson);
}
static void testPhoneNumber() throws JAXBException {
System.out.println("phoneNumber:");
PersonEntity personEntity = createPersonEntity();
PhoneNumberEntity phoneNumberEntity = personEntity.getPhoneNumbers().get(0);
String entityJson = marshal(phoneNumberEntity);
System.out.println(entityJson);
unmarshal(PhoneNumberEntity.class, entityJson);
}
static PersonEntity createPersonEntity() {
PersonEntity person = new PersonEntity();
person.setFirstname("yvonne");
PhoneNumberEntity firstPhoneNumber = new PhoneNumberEntity();
firstPhoneNumber.setNumber("12345");
firstPhoneNumber.setPerson(person);
PhoneNumberEntity secondPhoneNumber = new PhoneNumberEntity();
secondPhoneNumber.setNumber("54321");
secondPhoneNumber.setPerson(person);
person.addPhoneNumber(firstPhoneNumber);
person.addPhoneNumber(secondPhoneNumber);
return person;
}
static String marshal(Object toMarshal) throws JAXBException {
JAXBContext jc = JAXBContextFactory.createContext(new Class[] {toMarshal.getClass()}, null);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
// marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_JSON);
marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_XML);
StringWriter sw = new StringWriter();
marshaller.marshal(toMarshal, sw);
return sw.toString();
}
static <T> T unmarshal(Class<T> entityClass, String str) throws JAXBException {
JAXBContext jc = JAXBContextFactory.createContext(new Class[] {entityClass}, null);
Unmarshaller unmarshaller = jc.createUnmarshaller();
// unmarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_JSON);
unmarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_XML);
return (T) unmarshaller.unmarshal(new StringReader(str));
}
}