Unmarshal a single element list fails

柔情痞子 提交于 2019-12-23 18:09:51

问题


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;
    }

}

回答1:


I have confirmed the issue that you are seeing and have opened the following bug:

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

UPDATE

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


来源:https://stackoverflow.com/questions/24910193/jaxbmarshaller-and-jaxbunmarshaller-are-not-compatible-with-each-other

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!