Spring RestTemplate and generic types ParameterizedTypeReference collections like List

前端 未结 6 677
一生所求
一生所求 2020-12-05 09:44

An Abstract controller class requires List of objects from REST. While using Spring RestTemplate its not mapping it to required class instead it returns Linked HashMAp

6条回答
  •  日久生厌
    2020-12-05 10:23

    I did this a bit different. In my situation, I had a base class where I was implementing a set of CRUD operations and then using derived classes to implement specific resource types.

    In the base class, I was trying to define a ParameterizedTypeReference as follows:

    ParameterizedTypeReference> typeRef = 
      new ParameterizedTypeReference>() {};
    

    This didn't work so I ended up creating an abstract method in the base class:

    protected abstract ParameterizedTypeReference> 
    getServicePagedResultTypeRef();
    

    and then in the derived classes:

    @Override
    protected ParameterizedTypeReference>
    getServicePagedResultTypeRef() {
      return new ParameterizedTypeReference>() {};
    }
    

    I could then use that in the base class like:

    ResponseEntity> response = lbRestTemplate.exchange(
      uri, HttpMethod.GET, null, getServicePagedResultTypeRef(), uriVariables);
    

提交回复
热议问题