Spring Framework
  1. Spring Framework
  2. SPR-7023

Proper handling of parameterized types in RestTemplate

    Details

    • Type: New Feature New Feature
    • Status: Resolved
    • Priority: Minor Minor
    • Resolution: Complete
    • Affects Version/s: 3.1.1
    • Fix Version/s: 3.2 M2
    • Component/s: Web
    • Labels:
      None
    • Last commented by a User:
      false

      Description

      Currently, the RestTemplate has no way to handle generic types. In other words, the following does not work:

      List<MyDomain> myDomainList = restTemplate.getForObject(uri, List.class);
      

      This should be fixed.

        Issue Links

          Activity

          Hide
          Fotios Lioutsis added a comment -

          I am using version 3.0.3.RELEASE and found that the description above works (since SPR-7002 is resolved). However there is no support for usage of RestTemplate getForObject with a Jackson JavaType instead. Passing responseType object as List.class does not use generics (like List<MyDomain> as response type argument in your example). There should be support as in ObjecMapper method:
          public <T> T readValue(Reader src,JavaType valueType)
          Perhaps the description should be updated with this in mind.

          Show
          Fotios Lioutsis added a comment - I am using version 3.0.3.RELEASE and found that the description above works (since SPR-7002 is resolved). However there is no support for usage of RestTemplate getForObject with a Jackson JavaType instead. Passing responseType object as List.class does not use generics (like List<MyDomain> as response type argument in your example). There should be support as in ObjecMapper method: public <T> T readValue(Reader src,JavaType valueType) Perhaps the description should be updated with this in mind.
          Hide
          Fotios Lioutsis added a comment -

          Clarification to the above comment: it is still failing with ClassCastException unless the workaround of SPR-7002 is used.

          Show
          Fotios Lioutsis added a comment - Clarification to the above comment: it is still failing with ClassCastException unless the workaround of SPR-7002 is used.
          Hide
          Jay Xu added a comment -

          This scenario exposes the limitation of type-erasing of Java generic handling. Looking forward to solution

          Show
          Jay Xu added a comment - This scenario exposes the limitation of type-erasing of Java generic handling. Looking forward to solution
          Hide
          Mukta added a comment -

          Is a fix for this planned in any of the upcoming releases?
          Is there a workaround in the time being? Thanks.

          Show
          Mukta added a comment - Is a fix for this planned in any of the upcoming releases? Is there a workaround in the time being? Thanks.
          Hide
          Rossen Stoyanchev added a comment -

          Commit ed3823b045fd19cdb801609eb034c93dd4d75c3f:

          This change makes it possible to use the RestTemplate to read an HTTP
          response into a target generic type object. The RestTemplate has three
          new exchange() methods that accept ParameterizedTypeReference -- a
          new class that enables capturing and passing generic type info.
          See the Javadoc of the three new methods in RestOperations for a
          short example.
          
          To support this feature, the HttpMessageConverter is now extended by
          GenericHttpMessageConverter, which adds a method for reading an
          HttpInputMessage to a specific generic type. The new interface
          is implemented by the MappingJacksonHttpMessageConverter and also by a
          new Jaxb2CollectionHttpMessageConverter that can read read a generic
          Collection where the generic type is a JAXB type annotated with
          @XmlRootElement or @XmlType.
          
          Show
          Rossen Stoyanchev added a comment - Commit ed3823b045fd19cdb801609eb034c93dd4d75c3f : This change makes it possible to use the RestTemplate to read an HTTP response into a target generic type object. The RestTemplate has three new exchange() methods that accept ParameterizedTypeReference -- a new class that enables capturing and passing generic type info. See the Javadoc of the three new methods in RestOperations for a short example. To support this feature, the HttpMessageConverter is now extended by GenericHttpMessageConverter, which adds a method for reading an HttpInputMessage to a specific generic type. The new interface is implemented by the MappingJacksonHttpMessageConverter and also by a new Jaxb2CollectionHttpMessageConverter that can read read a generic Collection where the generic type is a JAXB type annotated with @XmlRootElement or @XmlType.

            People

            • Assignee:
              Rossen Stoyanchev
              Reporter:
              Arjen Poutsma
              Last updater:
              Rossen Stoyanchev
            • Votes:
              14 Vote for this issue
              Watchers:
              16 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Days since last comment:
                1 year, 33 weeks, 5 days ago