I'm running a sample (which i can't find anymore) from Blaise Doughans blog on Glassfish 3 using EclipseLink 2.5 MOXy for JAXB service.
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Company {
@XmlElementWrapper(name="employees")
@XmlElement(name = "employee", type=Employee.class)
private List<Employee> employees;
}
@XmlAccessorType(XmlAccessType.FIELD)
public class Employee {
private String id;
private String name;
}
I added some annotations to the classes, to produce the desired json structure:
{
"employees": [
{
"id": "1",
"name": "Jane Doe",
"report": []
}
]
}
When i try to unmarshal this JSON it sadly fails, returning an object with an empty employees list.
Adding another element to the JSON list OR removing the @XmlElementWrapper
works.
But i want the key element to be named employees
, so i have to use the wrapper annotation, or not?
Edit:
public class MyApplication extends Application {
@Override
public Set<Class<?>> getClasses() {
HashSet<Class<?>> set = new HashSet<Class<?>>(2);
set.add(MOXyJsonProvider.class);
set.add(Index.class);
return set;
}
@Override
public Set<Object> getSingletons() {
MOXyJsonProvider moxyJsonProvider = new MOXyJsonProvider();
moxyJsonProvider.setAttributePrefix("@");
moxyJsonProvider.setFormattedOutput(true);
moxyJsonProvider.setIncludeRoot(false);
moxyJsonProvider.setMarshalEmptyCollections(true);
moxyJsonProvider.setValueWrapper("$");
moxyJsonProvider.setWrapperAsArrayName(true);
HashSet<Object> set = new HashSet<Object>(1);
set.add(moxyJsonProvider);
return set;
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…