title: Consuming Rest api with array list of xml response using spring java
body: <p>I'm trying to consume REST API which gives me XML response.I'm mapping XML response to POJO file.While the outer most XML element is mapped properly,the inner nested array element is returning null values. Following are the request response and other code files : </p>
  
  <p>XML Response:</p>
  
  <pre><code>&lt;ArrayOfRouteListItem
      xmlns="http://some url..."
      xmlns:i="http://url..."&gt;
      &lt;RouteListItem&gt;
          &lt;ArrivalCountry&gt;(Saronic Islands, Greece)&lt;/ArrivalCountry&gt;
          &lt;ArrivalPort&gt;Agistri&lt;/ArrivalPort&gt;
          &lt;DepartCountry&gt;(Saronic Islands, Greece)&lt;/DepartCountry&gt;
          &lt;DepartPort&gt;Aegina&lt;/DepartPort&gt;
          &lt;HeaderKey&gt;HA&lt;/HeaderKey&gt;
          &lt;ReturnFlag&gt;R&lt;/ReturnFlag&gt;
      &lt;/RouteListItem&gt;
      &lt;RouteListItem&gt;
          &lt;ArrivalCountry&gt;(Saronic Islands, Greece)&lt;/ArrivalCountry&gt;
          &lt;ArrivalPort&gt;Aegina&lt;/ArrivalPort&gt;
          &lt;DepartCountry&gt;(Greece)&lt;/DepartCountry&gt;
          &lt;DepartPort&gt;Piraeus&lt;/DepartPort&gt;
          &lt;HeaderKey/&gt;
          &lt;ReturnFlag&gt;R&lt;/ReturnFlag&gt;
      &lt;/RouteListItem&gt;
  &lt;/ArrayOfRouteListItem&gt;
  </code></pre>
  
  <p>POJO class  ArrayofRouteListItem.java :</p>
  
  <pre><code>@XmlAccessorType(XmlAccessType.FIELD)    
  @XmlRootElement(name="ArrayOfRouteListItem" , namespace="http://some url ...")
  public class ArrayOfRouteListItem {     
  @XmlElement(name = "RouteListItem")     
  public List&lt;RouteListItem&gt; routeItem = new ArrayList&lt;RouteListItem&gt;();
  }
  </code></pre>
  
  <p>POJO Class RouteListItem.java</p>
  
  <pre><code>@XmlRootElement(name="RouteListItem")   
   @XmlAccessorType(XmlAccessType.FIELD)       
  @XmlType(namespace="http://some url ..")
      public class RouteListItem implements Serializable {
      public static final long serialVersionUID = 1L;
  
      @XmlElement(name="ArrivalCountry")
      public String arrivalCountry;
  
      @XmlElement(name="ArrivalPort")
      public String arrivalPort;
  
      @XmlElement(name="DepartCountry")
      public String departCountry;
  
      @XmlElement(name="DepartPort")
      public String departPort;
  
      @XmlElement(name="HeaderKey")
      public String headerKey;
  
      @XmlElement(name="ReturnFlag")
      public String returnFlag;
      ......
  
  }
  </code></pre>
  
  <p>MY Main Application (Controller.java):</p>
  
  <pre><code>   public class Controller {
  
      private static DefaultHttpClient httpClient = new DefaultHttpClient();
      public static void main(String[] args) throws Exception 
      {
          demoGetRESTAPI();
  
      }
      public static void demoGetRESTAPI() throws Exception 
      {
          try
          {
              HttpGet getRequest = new HttpGet("https://www.url...");
              getRequest.addHeader("accept", "application/xml");
              HttpResponse response = httpClient.execute(getRequest);
              int statusCode = response.getStatusLine().getStatusCode();
              if (statusCode != 200) 
              {
                  throw new RuntimeException("Failed with HTTP error code : " + statusCode);
              }
              HttpEntity httpEntity = response.getEntity();
              String apiOutput = EntityUtils.toString(httpEntity);
              System.out.println("xml file content : " + apiOutput); 
  
              JAXBContext jaxbContext = JAXBContext.newInstance(ArrayOfRouteListItem.class);
              Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
              StringReader reader = new StringReader(apiOutput);
              System.out.println("reader object :  " + reader.toString());
              ArrayOfRouteListItem routeList = (ArrayOfRouteListItem) unmarshaller.unmarshal(reader);
              Marshaller marshaller = jaxbContext.createMarshaller();
              marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
              marshaller.marshal(routeList, System.out);
  
              System.out.println("size: " + routeList.getRouteItem().size() + "  .get(0)    :     " + routeList.getRouteItem().get(0).getArrivalCountry());
            }
          finally
          {
              //Important: Close the connect
              httpClient.getConnectionManager().shutdown();
          }
      }
  </code></pre>
  
  <p>The Output which I'm getting are :</p>
  
  <pre><code>reader object :  java.io.StringReader@38c5cc4c
  &lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
  &lt;ns2:ArrayOfRouteListItem   xmlns:ns2="http://some url..."&gt;
  &lt;RouteListItem/&gt;
  &lt;RouteListItem/&gt;
  &lt;RouteListItem/&gt;
  &lt;RouteListItem/&gt;
  &lt;RouteListItem/&gt;
  &lt;RouteListItem/&gt;
  &lt;/ns2:ArrayOfRouteListItem&gt;
  routeList.size: 6    routeList.getRouteItem().get(0).getArrivalCountry() : null
  </code></pre>