Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Rename outer XML tag for collection

 
Peter Johnson
author
Bartender
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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):



 
Tim Moores
Saloon Keeper
Posts: 3266
55
  • Mark post as helpful
  • send pies
  • 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
Bartender
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The XmlElementWrapper is not a valid class annotation (though that would have been a simple solution).
 
Stephan van Hulst
Bartender
Posts: 6587
86
  • Mark post as helpful
  • send pies
  • 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
Bartender
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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
Bartender
Posts: 6587
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using Response, and not just the actual thing you want to serialize?
 
Peter Johnson
author
Bartender
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!