Jersey XmlRootElement GenericeEntity和Resteasy

Jersey XmlRootElement GenericeEntity和Resteasy,jersey,jax-rs,resteasy,java-ee-7,Jersey,Jax Rs,Resteasy,Java Ee 7,在我的JavaEE项目中,我正在使用JAX-RS。有时我需要返回一个集合。其想法是尽可能保持实现的通用性,以便我可以轻松地在不同的应用程序服务器之间部署。当我在列表中使用GenericEntity时,我在Resteasy(Wildfly)和Jersey(Glassfish)上得到了两个不同的xmlRootElement。我的切入点: @Path("/example") @RequestScoped public class ExampleResponse { @GET @Pro

在我的JavaEE项目中,我正在使用JAX-RS。有时我需要返回一个集合。其想法是尽可能保持实现的通用性,以便我可以轻松地在不同的应用程序服务器之间部署。当我在
列表中使用
GenericEntity
时,我在Resteasy(Wildfly)和Jersey(Glassfish)上得到了两个不同的xmlRootElement。我的切入点:

@Path("/example")
@RequestScoped
public class ExampleResponse {

    @GET
    @Produces({MediaType.APPLICATION_XML})
    public Response getResponse() {
        List<TestEntity> entities = new ArrayList<>();
        entities.add(new TestEntity());
        entities.add(new TestEntity());
        entities.add(new TestEntity());

        GenericEntity<List<TestEntity>> generic = new GenericEntity<List<TestEntity>>(entities){};

        return Response.ok().entity(generic).build();
    }
}
@Path(“/示例”)
@请求范围
公共类示例响应{
@得到
@产生({MediaType.APPLICATION_XML})
公共响应getResponse(){
列表实体=新的ArrayList();
添加(newtestentity());
添加(newtestentity());
添加(newtestentity());
GenericEntity generic=新的GenericEntity(实体){};
返回Response.ok().entity(generic.build();
}
}
Resteasy的结果:

<collection> <!--root element collection-->
    <testEntity>
        <val1>Hi</val1>
        <val2>Bye</val2>
    </testEntity>
    <testEntity>
        <val1>Hi</val1>
        <val2>Bye</val2>
    </testEntity>
    <testEntity>
        <val1>Hi</val1>
        <val2>Bye</val2>
    </testEntity>
</collection>

你好
拜伊
你好
拜伊
你好
拜伊
球衣的结果:

<testEntities> <!--root element plural-->
    <testEntity>
        <val1>Hi</val1>
        <val2>Bye</val2>
    </testEntity>
    <testEntity>
        <val1>Hi</val1>
        <val2>Bye</val2>
    </testEntity>
    <testEntity>
        <val1>Hi</val1>
        <val2>Bye</val2>
    </testEntity>
</testEntities>

你好
拜伊
你好
拜伊
你好
拜伊

是否可以通过配置将多个根元素与Resteasy一起使用,或将集合根元素与Jersey一起使用?我知道可以创建一个包装类来完成这项工作,但这似乎不是一个可行的方法。

对于Jersey,它看起来像没有可配置属性的行为。查看RESTeasy源代码,它看起来像方法上的注释。“别引用我的话,我从来没有试过。”皮斯基勒非常感谢你的回答。
@Wrapper
是一个Resteasy特有的注释,我尽量避免使用它。为了获得安全/一致的响应,我可能必须编写包装器类。我能想到的一个消除包装器类的可移植解决方案是使用。它支持JAXB注释(大多数情况下)。您必须弄清楚如何使用Jersey禁用Glassfish和Wildfly中的提供程序(如果它们还没有通过添加其他提供程序来禁用),这看起来像是没有可配置属性的行为。查看RESTeasy源代码,它看起来像是方法上的注释。“别引用我的话,我从来没有试过。”皮斯基勒非常感谢你的回答。
@Wrapper
是一个Resteasy特有的注释,我尽量避免使用它。为了获得安全/一致的响应,我可能必须编写包装器类。我能想到的一个消除包装器类的可移植解决方案是使用。它支持JAXB注释(大多数情况下)。您必须弄清楚如何禁用Glassfish和Wildfly中的提供程序(如果它们还没有通过添加其他提供程序禁用)