If I have to marshal a List<?>
how to avoid that it shows the type?
So the result of a marshalling List<?>
is [{"type" : "person","id":"1"},{"type" : "person","id":"2"}] }
and it give me also the type="Person" in the JSON results!
How could I avoid that it shows me the type?
Thank you
I haven't been able to reproduce the issue that you are seeing. Below is what I have tried.
Domain Model (Person)
package forum16966861;
public class Person {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
jaxb.properties
To specify MOXy as your JAXB provider you need to include a file called jaxb.properties
in the same package as your domain model with the following entry (see: http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html).
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
Demo
package forum16966861;
import java.util.*;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextProperties;
public class Demo {
public static void main(String[] args) throws Exception {
Map<String, Object> properties = new HashMap<String, Object>(2);
properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false); JAXBContext jc = JAXBContext.newInstance(new Class[] {Person.class}, properties);
List<Object> people = new ArrayList<Object>(2);
Person jane = new Person();
jane.setId(1);
jane.setName("Jane");
people.add(jane);
Person john = new Person();
john.setId(2);
john.setName("John");
people.add(john);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(people, System.out);
}
}
Output
[ {
"id" : 1,
"name" : "Jane"
}, {
"id" : 2,
"name" : "John"
} ]
UPDATE
I don't have right now the code but I can see that the difference is
that I define a metadata xml file where I say how to bind Person.
I still haven't reproduced your issue, but here is how I have adapted my example.
oxm.xml
<?xml version="1.0"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="forum16966861">
<java-types>
<java-type name="Person">
<xml-root-element/>
</java-type>
</java-types>
</xml-bindings>
Demo
package forum16966861;
import java.util.*;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextProperties;
public class Demo {
public static void main(String[] args) throws Exception {
Map<String, Object> properties = new HashMap<String, Object>(3);
properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "forum16966861/oxm.xml");
properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
JAXBContext jc = JAXBContext.newInstance("forum16966861", Person.class.getClassLoader(), properties);
List<Object> people = new ArrayList<Object>(2);
Person jane = new Person();
jane.setId(1);
jane.setName("Jane");
people.add(jane);
Person john = new Person();
john.setId(2);
john.setName("John");
people.add(john);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(people, System.out);
}
}
Output
[ {
"id" : 1,
"name" : "Jane"
}, {
"id" : 2,
"name" : "John"
} ]