0

I am trying to make a web service with Jax-RS (using the Glassfish 3.1.1, all RI). The Question class is annotated with @XmlRootElement and have this method. I have not annotated it with any Jax-RS annotations. What I expected was that you would get one answers-element that had all the answer-elements inside...but that is not the case obviously. How do I that? The Answer class is also annotated with @XmlRootElement.

@OneToMany(cascade = CascadeType.ALL)
public List<Answer> getAnswers() {
    return answers;
}

This is the response:

<questions>
<question>
    <answers>
        <correct>true</correct>
        <description>Answer one</description>
        <id>1</id>
    </answers>
    <answers>
        <correct>false</correct>
        <description>Answer two</description>
        <id>2</id>
    </answers>
    <description>Question One</description>
    <id>1</id>
    <imageName>hello.png</imageName>
    <status>SUBMITTED</status>
</question>
...
</questions>
3
  • Please consider showing the other classes you referenced. If the problem exists in that code, we can't help you spot it :( Maybe also show an example of the response you're expecting. Commented Apr 8, 2012 at 19:43
  • But I already gave you the response? it is the XML. Commented Apr 8, 2012 at 19:44
  • Sorry, it wasn't clear to me what you were trying to do, as you are mentioning other classes -- like the elusive Questions class -- that you didn't list. It looks like someone was able to answer though. :) Commented Apr 8, 2012 at 19:49

1 Answer 1

1

The annotation you're looking for is XMLElementWrapper

@XMLElementWrapper("answers")
@XMLElement("answer")
private List<Answer> answers;

Should give you

<answers>
    <answer>...</answer>
    <answer>...</answer>
</answer>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.