• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Rename outer XML tag for collection

 
author
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm using Jersey 2. I have a class such as:



When I GET a single student, the XML returned by Jersey looks like:



But when I get a collection of student, the XML returned by Jersey looks like:



How would I go about convincing Jersey (or JAXB) to instead return (i.e., how to rename the base collection tag):



 
Saloon Keeper
Posts: 7592
177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I haven't had to do this myself, but I think the solution involves an XmlElementWrapper annotation.
 
Peter Johnson
author
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The XmlElementWrapper is not a valid class annotation (though that would have been a simple solution).
 
Saloon Keeper
Posts: 15554
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't use it on a class. You use it on a field or method that returns a collection of your type.
 
Peter Johnson
author
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Stephan, I know that, and when I have a collection embedded within another class, I used that annotation there. But here, I am asking about the XML being returned when I do a "GET /students" call and the server code, to return the collection of students, does this:


 
Stephan van Hulst
Saloon Keeper
Posts: 15554
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you using Response, and not just the actual thing you want to serialize?
 
Peter Johnson
author
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to handle any exceptions thrown by the code, in which case a different object is returned (this object is used specifically to wrap the error message). So all of my methods can return either the requested object or the error object. I don't want the stack trace or other exception paraphernalia being returned for an error. And to manage that without having error handling boilerplate code present for every request (I inherited a project that did that, what a mess), I have a centralized method that all request go through. But your code has given me an idea, let me see if that works.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you found the solution?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic