title: Spring MVC Test with RestTemplate: Generic collection fails (even with ParameterizedTypeReference)
body: <p>I am working with Spring Framework <code>4.3.1</code></p>
  
  <p>I have the following domain class</p>
  
  <pre><code>@XmlRootElement(name="persona")
  @XmlType(propOrder = {"id","nombre","apellido","fecha"})
  public class Persona implements Serializable {
  
      @XmlElement(name="id")
      @JsonProperty("id")
      public String getId() {
          return id;
      }
  
      ....
  </code></pre>
  
  <p>Where each getter has the <code>@XmlElement</code> and <code>@JsonProperty</code> annotations.
  I am working with <code>JAXB2</code> and <code>Jackson2</code></p>
  
  <p>I have the following too:</p>
  
  <pre><code>@XmlRootElement(name="collection")
  public class GenericCollection&lt;T&gt; {
  
      private Collection&lt;T&gt; collection;
  
      public GenericCollection(){
  
      }
  
      public GenericCollection(Collection&lt;T&gt; collection){
          this.collection = collection;
      }
  
      @XmlElement(name="item")
      @JsonProperty("collection")
      public Collection&lt;T&gt; getCollection() {
          return collection;
      }
  
      public void setCollection(Collection&lt;T&gt; collection) {
          this.collection = collection;
      }
  
      @Override
      public String toString() {
        StringBuilder builder = new StringBuilder();
        for(Object object : collection){
          builder.append("[");
          builder.append(object.toString());
          builder.append("]");
        }
        return builder.toString();
      }
  
  }
  </code></pre>
  
  <p>About Testing, the many <code>@Test</code>s methods working through <code>Spring MVC Test</code> work fine. The <code>@Controller</code> and <code>@RestController</code> work how is expected. </p>
  
  <p><strong>Note</strong>: I can test the CRUD scenarios, it about the <code>HTTP methods</code> such as <code>POST</code>, <code>PUT</code>, <code>GET</code> and <code>DELETE</code>. Therefore I am able to get one entity and a collection of entities. </p>
  
  <p><strong>Note</strong>: from the previous note, all works working around the <code>XML</code> and <code>JSON</code> formats.</p>
  
  <p>Now trying to do testing through the <code>RestTemplate</code> how a kind of programmatic client, it <strong>only</strong> <em>fails</em> for collections. With the following:</p>
  
  <pre><code>@Before
  public void setUp(){
      mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
      restTemplate = new RestTemplate(new MockMvcClientHttpRequestFactory(mockMvc));
  
      List&lt;HttpMessageConverter&lt;?&gt;&gt; converters = new ArrayList&lt;&gt;();
      converters.add(httpMessageConverterConfig.marshallingMessageConverter());
      converters.add(httpMessageConverterConfig.mappingJackson2HttpMessageConverter());
  
      restTemplate.setMessageConverters(converters);
      System.out.println("converters.size():" + converters.size());
  }
  </code></pre>
  
  <p>I can confirm <code>converters.size()</code> always prints <code>2</code></p>
  
  <p>The following is for XML and JSON</p>
  
  <pre><code>@Test
  public void findAllXmlTest(){
  
      RequestEntity&lt;Void&gt; requestEntity = RestControllerSupport_.createRequestEntityForGet(uri, retrieveURI);
  
      ParameterizedTypeReference&lt;GenericCollection&lt;Persona&gt;&gt; parameterizedTypeReference = new ParameterizedTypeReference&lt;GenericCollection&lt;Persona&gt;&gt;(){};
      ResponseEntity&lt;GenericCollection&lt;Persona&gt;&gt; responseEntity = restTemplate.exchange(requestEntity, parameterizedTypeReference);
  
      assertThat(responseEntity, notNullValue());
  
      assertThat(responseEntity.getStatusCode(), is(HttpStatus.OK));
      assertThat(responseEntity.getHeaders().getContentType(), is(MediaType.APPLICATION_XML) );
  
      assertThat(responseEntity.getBody(), notNullValue());
      assertThat(responseEntity.getBody().getClass(), is(GenericCollection.class));
      assertThat(responseEntity.getBody().getCollection(), is(personas));
  
  }
  
  @Test
  public void findAllJsonTest(){
  
      RequestEntity&lt;Void&gt; requestEntity = RestControllerSupport_.createRequestEntityForGet(uri, retrieveURI);
  
      ParameterizedTypeReference&lt;GenericCollection&lt;Persona&gt;&gt; parameterizedTypeReference = new ParameterizedTypeReference&lt;GenericCollection&lt;Persona&gt;&gt;(){};
      ResponseEntity&lt;GenericCollection&lt;Persona&gt;&gt; responseEntity = restTemplate.exchange(requestEntity, parameterizedTypeReference);
  
      assertThat(responseEntity, notNullValue());
  
      assertThat(responseEntity.getStatusCode(), is(HttpStatus.OK));
      assertThat(responseEntity.getHeaders().getContentType(), is(MediaType.APPLICATION_JSON_UTF8) );
  
      assertThat(responseEntity.getBody(), notNullValue());
      assertThat(responseEntity.getBody().getClass(), is(GenericCollection.class));
      assertThat(responseEntity.getBody().getCollection(), is(personas));
  
  }
  </code></pre>
  
  <p><strong>Note:</strong> observe I am using <code>ParameterizedTypeReference</code> for both scenarios.</p>
  
  <p>For JSON it works.</p>
  
  <p>But for XML I get:</p>
  
  <pre><code>org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [com.manuel.jordan.controller.support.GenericCollection&lt;com.manuel.jordan.domain.Persona&gt;] and content type [application/xml]
      at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:109)
  </code></pre>
  
  <p>What is wrong or missing?</p>