title: How to annotate a list using @XmlElement?
body: <p>I have the following annotation using <code>javax.xml.bind.annotation.XmlElement</code>:</p>
  
  <pre><code>@XmlElement         
  public List&lt;String&gt; getKeywords() {
      return keywords;
  }
  </code></pre>
  
  <p>Which produces the following XML when I marshall some example content:</p>
  
  <pre><code>&lt;keywords&gt;keyword1&lt;/keywords&gt;
  &lt;keywords&gt;keyword2&lt;/keywords&gt;
  </code></pre>
  
  <p>I would like to get the following XML:</p>
  
  <pre><code>&lt;keywords&gt;
      &lt;keyword&gt;keyword1&lt;/keyword&gt;
      &lt;keyword&gt;keyword2&lt;/keyword&gt;
  &lt;/keywords&gt;
  </code></pre>
  
  <p>What kind of an annotation should I use?</p>
  
  <p>I've tried</p>
  
  <pre><code>@XmlElementWrapper
  @XmlElement(name="keyword")
  </code></pre>
  
  <p>But then the whole content disappears and the result is:</p>
  
  <pre><code>&lt;keywords/&gt;
  </code></pre>
  
  <p>The same happens also if I only try to rename the element:</p>
  
  <pre><code>@XmlElement(name="keyword")
  </code></pre>
  
  <p>What am I doing wrong?</p>
  
  <p>UPDATE:</p>
  
  <p>Here is the updated full code for the class according to the first answers, but it is still not working (the result is an empty list <code>&lt;keywords/&gt;</code> when marshalled to XML):</p>
  
  <pre><code>import java.util.List;
  import javax.xml.bind.annotation.XmlElement;
  import javax.xml.bind.annotation.XmlElementWrapper;
  import javax.xml.bind.annotation.XmlRootElement;
  
  @XmlRootElement
  public class Content {
  
      private List&lt;String&gt; keywords;
  
      public Content() {}
  
      @XmlElementWrapper(name="keywords")
      @XmlElement(name="keyword")
      public List&lt;String&gt; getKeywords() {
          return keywords;
      }
  
      public void setKeywords(List&lt;String&gt; keywords) {
          this.keywords = keywords;
      }  
  
  }
  </code></pre>
  
  <p>I also tried the following with the same result:</p>
  
  <pre><code>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.XmlElementWrapper;
  import javax.xml.bind.annotation.XmlRootElement;
  
  @XmlRootElement
  @XmlAccessorType(XmlAccessType.FIELD)
  public class Content {
  
      @XmlElementWrapper(name="keywords")
      @XmlElement(name="keyword")
      private List&lt;String&gt; keywords;
  
      public Content() {}
  
      public List&lt;String&gt; getKeywords() {
          return keywords;
      }
  
      public void setKeywords(List&lt;String&gt; keywords) {
          this.keywords = keywords;
      }  
  
  }
  </code></pre>
  
  <p>However, the keywords are not empty as the following produces <code>&lt;keywords&gt;keyword1&lt;/keywords&gt;&lt;keywords&gt;keyword2&lt;/keywords&gt;</code> instead of an empty list:</p>
  
  <pre><code>import java.util.List;
  import javax.xml.bind.annotation.XmlElement;
  import javax.xml.bind.annotation.XmlElementWrapper;
  import javax.xml.bind.annotation.XmlRootElement;
  
  @XmlRootElement
  public class Content {
  
      private List&lt;String&gt; keywords;
  
      public Content() {}
  
      @XmlElement
      public List&lt;String&gt; getKeywords() {
          return keywords;
      }
  
      public void setKeywords(List&lt;String&gt; keywords) {
          this.keywords = keywords;
      }  
  
  }
  </code></pre>
  
  <p>The code for marshalling is (JAX-RS):</p>
  
  <pre><code>import java.io.StringWriter;
  import javax.ws.rs.Consumes;
  import javax.ws.rs.Path;
  import javax.ws.rs.POST;
  import javax.ws.rs.Produces;
  import javax.ws.rs.core.MediaType;
  import javax.ws.rs.core.Response;
  import javax.xml.bind.JAXBContext;
  import javax.xml.bind.JAXBException;
  import javax.xml.bind.Marshaller;
  
  @Path("process")
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_XML)
  public class ContentHandler {
  
      @POST
      public Response process(Content content) {
  
          StringWriter stringWriter = new StringWriter();
          try {
              JAXBContext jaxbContext = JAXBContext.newInstance(Content.class);
              Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
              jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
              jaxbMarshaller.marshal(content, stringWriter);
          } catch (JAXBException e) {
              return Response.serverError().entity(e.getMessage()).build();
          }
          return Response.ok(stringWriter.toString(), MediaType.APPLICATION_XML).build();       
      }
  
  }
  </code></pre>