NOTE. . Although this answer works, anar answer is better.
You should try using the annotated JAXB class to solve your problem. You can change your method as follows:
@GET @Produces(MediaType.TEXT_XML) @Path("/directgroups") public Groups getDirectGroupsForUser(@PathParam("userId") String userId) { try { Groups groups = new Groups(); groups.getGroup().addAll(service.getDirectGroupsForUser(userId, null, true)); return groups; } catch (UserServiceException e) { LOGGER.error(e); throw new RuntimeException(e.getMessage()); } }
And then create an annotated JAXB class for your groups. I have included the generated class for you using the process described in this answer . Here is an example of the documents that he will produce:
<groups> <group>Group1</group> </group>Group2</group> </groups>
And here is the generated class:
package example; import java.util.ArrayList; import java.util.List; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "", propOrder = { "group" }) @XmlRootElement(name = "groups") public class Groups { @XmlElement(required = true) protected List<String> group; public List<String> getGroup() { if (group == null) { group = new ArrayList<String>(); } return this.group; } }
Christian trimble
source share