开发者

Root element name in collections returned by RESTEasy

开发者 https://www.devze.com 2023-03-09 07:41 出处:网络
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.

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>
     <pr开发者_运维百科ice>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).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号