gpt4 book ai didi

java - Jersey 与 RestEasy - JAX-RS XML 集合根元素名称更改

转载 作者:行者123 更新时间:2023-11-30 05:27:44 26 4
gpt4 key购买 nike

我有以下 Jax-RS 端点:

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

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

使用 Jersey 1.x,它曾经返回:

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

现在我使用 RestEasy,它返回:

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

如何控制返回的 GenericEntity<List<X>>根名称在 Jax-RS 中(使用 Rest-Easy)?

请注意,我还返回 Json 格式,并且需要保持 API 向后兼容(例如根元素是 Json 中的数组,应该保持不变)

最佳答案

在深入研究 RestEasy 源代码后,我自己找到了解决方案。您只需将 @Wrapped(element="___") 注释添加到该方法即可:

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

对于 XML 可以正常工作,对于 JSON 可以正确忽略。

关于java - Jersey 与 RestEasy - JAX-RS XML 集合根元素名称更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58214362/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com