解组单个元素列表失败 [英] Unmarshal a single element list fails

查看:115
本文介绍了解组单个元素列表失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在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:

  • http://bugs.eclipse.org/411001

UPDATE

此问题的修复程序已经检入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:

  • http://www.eclipse.org/eclipselink/downloads/nightly.php

这篇关于解组单个元素列表失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆