Unmarshal a single element list fails

2019-01-20 12:38发布

问题:

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