title: Spring MVC XML output from a RestController
body: <p>I've got a Spring Boot/MVC (v 1.3) app which produces some XML for a REST-y sort of web service.  </p>
  
  <p>The problem I have is that by and large, the results are in an ArrayList as they're a list of items, but it seems that the produced XML uses the get() method name of the class the list is in to drive what the individual items are called, instead of either the class name, or the name driven by an annotation on that class.</p>
  
  <p>Here's some demo code and results to see what I mean:</p>
  
  <pre><code>@RestController
  public class XMLTest {
  
      @RequestMapping("/xmltest1")
      public TestItem test() {
          return new TestItem("testData");
      }
  
      @RequestMapping("/xmltest2")
      public ArrayList&lt;TestItem&gt; test2() {
          ArrayList&lt;TestItem&gt; testItems = new ArrayList&lt;TestItem&gt;();
          testItems.add(new TestItem("testData"));
          testItems.add(new TestItem("testData2"));
          return testItems;
      }
  
      @RequestMapping("/xmltest3")
      public TestItems test3() {
          TestItems someTestItems = new TestItems();
          someTestItems.setNameOfAList(new ArrayList&lt;TestItem&gt;());
          someTestItems.getNameOfAListGetterMethod().add(new TestItem("testData"));
          someTestItems.getNameOfAListGetterMethod().add(new TestItem("testData2"));
          return someTestItems;
      }
  
      @JacksonXmlRootElement(localName = "testItemName")
      private class TestItem {
          public String itemData;
          public TestItem(String itemData) {
              this.itemData = itemData;
          }
      }
  
      @JacksonXmlRootElement(localName = "testItemListName")
      private class TestItems {
  
          private List&lt;TestItem&gt; nameOfAList = null;
  
          // This makes it not wrap the list twice, once for this class, and once for this list, which
          // you put on the getter as to demo it it has the "wrong" name, which is then used for each item.
          @JacksonXmlElementWrapper(useWrapping = false)
          public List&lt;TestItem&gt; getNameOfAListGetterMethod() {
              return nameOfAList;
          }
  
          public void setNameOfAList(List&lt;TestItem&gt; nameOfAList) {
              this.nameOfAList = nameOfAList;
          }
      }
  }
  </code></pre>
  
  <p>And the results are as follows:</p>
  
  <pre><code>$ http http://localhost:9081/xmltest1
  
  &lt;testItemName&gt;
      &lt;itemData&gt;testData&lt;/itemData&gt;
  &lt;/testItemName&gt;
  
  $ http http://localhost:9081/xmltest2
  
  &lt;ArrayList&gt;
      &lt;item&gt;
          &lt;itemData&gt;testData&lt;/itemData&gt;
      &lt;/item&gt;
      &lt;item&gt;
          &lt;itemData&gt;testData2&lt;/itemData&gt;
      &lt;/item&gt;
  &lt;/ArrayList&gt;
  
  $ http http://localhost:9081/xmltest3
  
  &lt;testItemListName&gt;
      &lt;nameOfAListGetterMethod&gt;
          &lt;itemData&gt;testData&lt;/itemData&gt;
      &lt;/nameOfAListGetterMethod&gt;
      &lt;nameOfAListGetterMethod&gt;
          &lt;itemData&gt;testData2&lt;/itemData&gt;
      &lt;/nameOfAListGetterMethod&gt;
  &lt;/testItemListName&gt;
  </code></pre>
  
  <p>As you can see in test3, it uses the getter method's name as the item, which by and large would reflect a plural (e.g. getItems() if you were getting a List type return), so you seem to have to go down an unnatural naming convention to get it to work.</p>
  
  <p>What I'd like.expect to be able to do is to have the actual TestItem node use the <code>@JacksonXmlRootElement(localName = "testItemName")</code> annotation to show it, but that doesn't seem to be possible?  I would imagine most people want to a) output sets of data as XML, and b) control how it appears, but I can't find any documentation or indeed questions about it.</p>
  
  <h1>Update</h1>
  
  <p>OK, I've found the answer(ish), which is to add</p>
  
  <pre><code>    @JacksonXmlProperty(localName = "testItemName")
  </code></pre>
  
  <p>to the get() method.  Odd, but does the trick, and looking through the source seems to be "by design" of sorts.  NB this is as well as the <code>@JacksonXmlElementWrapper(useWrapping = false)</code> annotation.</p>