Generic return object for a collection of objects in RESTful Spring3 app
I am looking for a generic way of returning a set of objects in my REST service. I use the Spring3 message-converters and the @ResponseBody annotation.
Some sites say that JAXB2 has "problems" returning a standard java.util.List, and all examples found define a dedicated class (e.g. Authors, Products, Customers) for each data domain, such as:
http://www.ibm.com/developerworks/web开发者_开发百科/library/wa-spring3webserv/index.html (see "Collection of resources", second paragraph)
Has anyone found a way to provide a generic class/approach for this that spares me creating all those collection classes?
Update 5th April 2011: I haven't really made any progress on this. Returning a generic list object always results in a JAXBException "class XXX nor any of its super class is known to this context"
When creating REST services in a JAX-RS framework (which uses JAXB for XML binding) you can return a List. For an example see:
- http://bdoughan.blogspot.com/2010/08/creating-restful-web-service-part-45.html
What happens when you try to do this with Spring?
You can just return a ModelMap and put whatever objects you want in there including arrays.
Wondering ... did you set the Content-Type : application/xml in the request headers?
精彩评论