我的课程结构如下:
@XmlRootElement(name="rootClass")
@XmlAccessorType(XmlAccessType.FIELD)
public class rootClass {
@XmlElementWrapper(name="children")
@XmlElement(name="child", type=childClass.class)
private List<childClass> children;
// Constructor, Getters, Setters
}
public class childClass {
private String name;
}
如果实例绑定到xml,它看起来像这样,完全如同:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<rootClass>
<children>
<child>
<name>Peter</name>
</child>
<child>
<name>Griffin</name>
</child>
</children>
</rootClass>
但在JSON中,它看起来像这样:
{
"children": {
"child": [
{
"name": "Peter"
},
{
"name": "Griffin"
}
]
}
}
虽然我希望它看起来像这样:
{
"children": [
{
"name": "Peter"
},
{
"name": "Griffin"
}
]
}
这种行为是由错误注释还是由于缺少注释参数引起的?
答案 0 :(得分:2)
注意:我是EclipseLink JAXB (MOXy)主管,是JAXB (JSR-222)专家组的成员。
以下是利用wrapperAsArrayName
属性将MOXy用作JSON绑定提供程序的方法:
import java.util.*;
import javax.ws.rs.core.Application;
import org.eclipse.persistence.jaxb.rs.MOXyJsonProvider;
public class MyApplication extends Application {
@Override
public Set<Class<?>> getClasses() {
HashSet<Class<?>> set = new HashSet<Class<?>>(1);
set.add(MyService.class);
return set;
}
@Override
public Set<Object> getSingletons() {
MOXyJsonProvider moxyJsonProvider = new MOXyJsonProvider();
moxyJsonProvider.setWrapperAsArrayName(true);
HashSet<Object> set = new HashSet<Object>(1);
set.add(moxyJsonProvider);
return set;
}
}
了解更多信息