Search code examples
listnestedjaxbeclipselink

Prevent list from being wrapped by XML-element


Example code:

@XmlRootElement(name="user")
public class User implements Serializable
{
    private long userid;
    private IPerson person; 
}

Produces this output:

<user>
    <person>
        <firstname />
        <lastname />
    </person>
</user>

I'd like to prevent the 'person'-element from being generated, so that the person-fields will be directly included in the 'user'-element, like this:

<user>
    <firstname />
    <lastname />
</user>

Can JAXB/EclipseLink be configured to produce this output?


Solution

  • You can use EclipseLink JAXB (MOXy)'s @XmlPath extension to map this use case:

    @XmlRootElement(name="user")
    public class User implements Serializable
    {
        private long userid;
    
        @XmlPath(".")
        private IPerson person; 
    }
    

    For More Information