RESTEasy返回的集合中的根元素名称 [英] Root element name in collections returned by RESTEasy

查看:138
本文介绍了RESTEasy返回的集合中的根元素名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JBoss AS 6中通过RestEasy使用JAX-RS。当我的JAX-RS资源返回一组项目时(例如通过List),RESTEasy总是使用名称集合作为根元素。



例如

 < ;收集> 
< item>
< description>计算机< / description>
< price> 2500< / price>
< / item>

< item>
< description> tv< / description>
< price> 1500< / price>
< / item>
< / collection>

此XML由例如:



$ b <$生成p $ p> @Produces(application / xml)
@Path(xml)
@RequestScoped
public class MyResource {

@GET
@Path(myitems)
public List< Item> getMyItems(){
return ...
}
}

可以看出,RESTEasy创建的根标记始终是< collection>



另一方面,泽西岛总是创建一个名称,该名称是列表中包含的元素的复数形式:

 <项目> 
< item>
< description>计算机< / description>
< price> 2500< / price>
< / item>

< item>
< description> tv< / description>
< price> 1500< / price>
< / item>
< / items>

我知道可以创建一个包装类型并返回而不是List,但这是一个相当的详细的解决方法,使代码更复杂。



是否可以轻松指定根标签的名称是什么?

解决方案

似乎是RTFM案例: RestEasy docs - JAXB对象的数组和集合


因此,如果我们想输出这个XML

 < foo:list xmlns:foo =http://foo.org> 
< customer>< name> bill< / name>< / customer>
< customer>< name> monica< / name>< / customer>
< / foo:list>

我们将使用@Wrapped注释
,如下所示:

  @GET 
@Path(list)
@Produces(application / xml)
@Wrapped( element =list,namespace =http://foo.org,prefix =foo)
public List< Customer> getCustomerSet(){...}


因此可以通过 @Wrapped 注释。这是一个RESTEasy特定的,但现在这样做。



保留问题以防万一有人有更好的解决方案(仍然在寻找一个全局拦截器orso,让RESTEasy可以做泽西所做的事情)。


I'm using JAX-RS via RestEasy in JBoss AS 6. When my JAX-RS resource returns a collection of items (e.g. via a List), RESTEasy always uses the name collection as the root element.

E.g.

<collection>    
   <item>
     <description>computer</description>
     <price>2500</price>
   </item>

   <item>
     <description>tv</description>
     <price>1500</price>
   </item>
</collection>

This XML is generated by e.g.:

@Produces("application/xml")
@Path("xml")
@RequestScoped
public class MyResource {

    @GET
    @Path("myitems")
    public List<Item> getMyItems() {
        return ...
    }
}

As can be seen the root tag that has been created by RESTEasy is always <collection>.

Jersey on the other hand always creates a name that is the plural form of the element contained in the list:

<items>    
   <item>
     <description>computer</description>
     <price>2500</price>
   </item>

   <item>
     <description>tv</description>
     <price>1500</price>
   </item>
</items>

I know it's possible to create a wrapper type and return that instead of a List, but that's a rather elaborate workaround and makes the code more complicated.

Is it possible to easily specify what the name of the root tag is for collections?

解决方案

Appeared to be a case of RTFM: RestEasy docs - Arrays and Collections of JAXB Objects

So, if we wanted to output this XML

<foo:list xmlns:foo="http://foo.org">
    <customer><name>bill</name></customer>
    <customer><name>monica</name></customer>
</foo:list>

We would use the @Wrapped annotation as follows:

@GET
@Path("list")
@Produces("application/xml")
@Wrapped(element="list", namespace="http://foo.org", prefix="foo")
public List<Customer> getCustomerSet() { ... }

It's thus possible via the @Wrapped annotation. It's a RESTEasy specific one, but this will do for now.

Leaving the question open in case someone has an even better solution (still looking for a global interceptor orso that lets RESTEasy do what Jersey does).

这篇关于RESTEasy返回的集合中的根元素名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆