Hey Forum!
I'm trying to use EclipseLink MOXy to marshal some models into XML. Therefor I would like to use ObjectGraphs to have a custom and different output of what is serialized into xml.
But when I try to implement the good examples of Blaise Doughan's blog (title: MOXy's Object Graphs - Input/Output Partial Models to XML & JSON) with Maven, I got following exception:
javax.xml.bind.PropertyException: name: eclipselink.object-graph value: contact info
Thats the used pom.xml:
<project xmlns="..." xmlns:xsi="..."
xsi:schemaLocation="... ...">
<modelVersion>4.0.0</modelVersion>
<groupId>main</groupId>
<artifactId>eclipselink_test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>eclipselink_test</name>
<url>link-to-maven</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.moxy</artifactId>
<version>2.5.1</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.2.11</version>
</dependency>
</dependencies>
</project>
This is the model (Model.java):
package main.eclipselink_test;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import org.eclipse.persistence.oxm.annotations.XmlNamedAttributeNode;
import org.eclipse.persistence.oxm.annotations.XmlNamedObjectGraph;
@XmlNamedObjectGraph(
name="contact info",
attributeNodes={
@XmlNamedAttributeNode("name")
})
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Model {
public Model() {
}
public Model(int id, String name) {
this.id = id;
this.name = name;
}
@XmlAttribute
private int id;
@XmlAttribute
private String name;
}
Thats the Application (App.java):
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import org.eclipse.persistence.jaxb.MarshallerProperties;
public class App
{
public static void main( String[] args ) throws JAXBException
{
JAXBContext jc = JAXBContext.newInstance(Model.class);
Model model = new Model(0, "testing");
// Output XML
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(model, System.out);
// Output XML - Based on Object Graph
marshaller.setProperty(MarshallerProperties.OBJECT_GRAPH, "contact info");
marshaller.marshal(model, System.out);
}
}
As you may see, I get the error message in App.java:
Exception in thread "main" javax.xml.bind.PropertyException: name: eclipselink.object-graph value: contact info
at javax.xml.bind.helpers.AbstractMarshallerImpl.setProperty(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.setProperty(Unknown Source)
at main.eclipselink_test.App.main(App.java:28)
(Sorry for the masses of code. I don't have the permission to post links.)
Does anybody have some suggestions what went wrong? Thanks!