I recently got rid of my XSD file and am using @XmlRootElement and @XmlElement to build my own Java representation classes. I had the same issue.. I had a <users> element, and inside I had one <user> returned. But, for some odd reason, two <user> elements would show up. I verified that I was only getting 1 user back from my query.
What I found out was that I had something like:
@XmlRootElement(name="users")
public class Users {
@XmlElement(name="user")
List<User> users = null;
public List<User> getUsers(){
return users;
}
public void setUsers(List<User> users){
this.users = users;
}
}
When I removed the @XmlElement(name="user"), my problem went away. But I was still getting something like:
<users>
<users>
<name>..</name>
</users>
</users>
I didn't realize the method name getUsers() would return the element as <users>. BEFORE I removed the @XmlElement(name="users"), I changed my method name to getUser().. which I hated because it wasn't representing that I was returning a list of users. So now I got the proper <users><user>... syntax.
What I learned was to do this:
@XmlElementRef
List<User> users = null;
By using @XmlElementRef, it picks up the object (User)'s @XmlRootElement name, instead of using the getter method in the Users class to determine the name.
HTH
--- On Thu, 12/2/10, sarat.pediredla <sarat.pediredla_at_gmail.com> wrote:
From: sarat.pediredla <sarat.pediredla_at_gmail.com>
Subject: [Jersey] JAXB marshalling creates duplicate elements
To: users_at_jersey.dev.java.net
Date: Thursday, December 2, 2010, 6:25 AM
I am posting it on the off-chance that somebody else might have some insight
into it. We are using Jersey with Hibernate entities to output XML and JSON.
Until quite recently, everything was fine and the XML/JSON produced was
perfect. However, due to some unknown reason, all the XML/JSON produced now
duplicates the "id" element twice.
I have made sure that,
1. Nothing has changed in the marshalling code. By default, we just use the
Jersey supplied JAXB marshaller.
2. Jersey version is 1.0 (same as before).
3. Checked the @XmlElement, @XmlAccessorType and @XmlType configurations on
the object to ensure nothing has changed. Also, I am pretty sure that JAXB
annotations would throw an error if I have duplicate elements in config.
Can anyone think of a reason why an element would be output twice?
--
View this message in context: http://jersey.576304.n2.nabble.com/JAXB-marshalling-creates-duplicate-elements-tp5796159p5796159.html
Sent from the Jersey mailing list archive at Nabble.com.