解组单个元素列表失败 [英] Unmarshal a single element list fails
问题描述
我正在 Glassfish 3 上运行来自 Blaise Doughans 博客的示例(我再也找不到了)将 EclipseLink 2.5 MOXy 用于 JAXB 服务.
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;
}
我在类中添加了一些注释,以生成所需的 json 结构:
I added some annotations to the classes, to produce the desired json structure:
{
"employees": [
{
"id": "1",
"name": "Jane Doe",
"report": []
}
]
}
当我尝试解组这个 JSON 时,它很遗憾地失败了,返回一个带有空员工列表的对象.
将另一个元素添加到 JSON 列表或删除 @XmlElementWrapper
有效.
但是我希望将关键元素命名为 employees
,所以我必须使用包装器注释,还是不?
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?
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;
}
}
推荐答案
我已确认您所看到的问题并已打开以下错误:
I have confirmed the issue that you are seeing and have opened the following bug:
更新
此问题的修复程序已检查到 EclipseLink 2.5.1 和 2.6.0 流中.从 2013 年 6 月 19 日开始,您可以从以下链接获得相应的夜间版本中的修复:
The fix for this issue has been checked into the EclipseLink 2.5.1 and 2.6.0 streams. You can get the fix in the corresponding nightly builds from the following link starting June 19, 2013:
这篇关于解组单个元素列表失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!