Search code examples
javaxmljerseyjax-rsresteasy

Jersey vs RestEasy - JAX-RS XML collection root element name change


I have the following Jax-RS end-point:

@XmlRootElement(name = "foobar")
public class Foobar {}

@GET
@Produces(MediaType.APPLICATION_XML)
public Object getFoobars() {
    return new GenericEntity<List<FooBar>>(service.getFooBars());
}

Using Jersey 1.x, it used to return:

<foobars>
  <foobar>...</foobar>
  <foobar>...</foobar>
</foobars>

Now that I use RestEasy, it returns:

<collection>
  <foobar>...</foobar>
  <foobar>...</foobar>
</collection>

How can I control the root name of a returned GenericEntity<List<X>> in Jax-RS (using Rest-Easy)?

Please note that I also return Json format and I need to keep the API backward-compatible (for exemple the root element is an array in Json and should stay the same)


Solution

  • Found the solution myself after digging a bit in the RestEasy source code. You just have to add the @Wrapped(element="___") annotation to the method:

    import org.jboss.resteasy.annotations.providers.jaxb.Wrapped;
    
    @GET
    @Produces(MediaType.APPLICATION_XML)
    @Wrapped(element = "foobars")
    public Object getFoobars() {
        return new GenericEntity<List<FooBar>>(service.getFooBars());
    }
    

    Works correctly for XML and properly ignored for JSON.