Vous pouvez utiliser l'extension @XmlNamedObjectGraph
de EclipseLink JAXB (MOXy) pour prendre en charge ce cas d'utilisation. Ce que @XmlNamedObjectGraph
vous permet de créer est plusieurs vues sur vos données.
Personne
Ci-dessous nous utiliserons @XmlNamedObjectGraph
pour créer une vue sur la classe Person
qui expose seulement 2 champs (firstName
et lastName
).
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.*;
@XmlNamedObjectGraph(
name = "simple",
attributeNodes = {
@XmlNamedAttributeNode("firstName"),
@XmlNamedAttributeNode("lastName")
}
)
@XmlAccessorType(XmlAccessType.FIELD)
public class Person {
private int clientId;
private String firstName;
private String lastName;
private String email;
public void setClientId(int clientId) {
this.clientId = clientId;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setEmail(String email) {
this.email = email;
}
}
Politique
Nous utiliserons également @XmlNamedObjectGraph
la classe Policy
. Il dit que pour le champ userCreated
appliquer le graphique d'objet nommé appelé simple
que nous avons défini sur la classe Person
.
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.*;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@XmlNamedObjectGraph(
name = "policy",
attributeNodes = {
@XmlNamedAttributeNode(value = "userCreated", subgraph = "simple"),
@XmlNamedAttributeNode("client")
}
)
public class Policy {
private Person userCreated;
private Person client;
public void setUserCreated(Person userCreated) {
this.userCreated = userCreated;
}
public void setClient(Person client) {
this.client = client;
}
}
Démo
Dans le code de démonstration ci-dessous nous indiquer le graphique d'objet nommé que nous voulons appliqué sur la Marshaller
en utilisant la propriété MarshallerProperties.OBJECT_GRAPH
.
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.MarshallerProperties;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Policy.class);
Person person = new Person();
person.setClientId(1234);
person.setFirstName("John");
person.setLastName("Doe");
person.setEmail("[email protected]");
Policy policy = new Policy();
policy.setClient(person);
policy.setUserCreated(person);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(MarshallerProperties.OBJECT_GRAPH, "policy");
marshaller.marshal(policy, System.out);
}
}
Sortie
est inférieure à la sortie de l'exécution du code de démonstration:
<?xml version="1.0" encoding="UTF-8"?>
<policy>
<userCreated>
<firstName>John</firstName>
<lastName>Doe</lastName>
</userCreated>
<client>
<clientId>1234</clientId>
<firstName>John</firstName>
<lastName>Doe</lastName>
<email>[email protected]</email>
</client>
</policy>
Pour plus d'informations
La classe 'Person' a des propriétés' clientId', 'email',' firstName', 'lastName' et selon l'endroit où l'objet apparaît voulez limiter les propriétés sont au format XML rangea? –
oui exactement c'est ce dont j'ai besoin – yglodt