55

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

 public List<T> restFindAll() {

    RestTemplate restTemplate = RestClient.build().restTemplate();
    ParameterizedTypeReference<List<T>>  parameterizedTypeReference = new ParameterizedTypeReference<List<T>>(){};
    String uri= BASE_URI +"/"+ getPath();

    ResponseEntity<List<T>> exchange = restTemplate.exchange(uri, HttpMethod.GET, null,parameterizedTypeReference);
    List<T> entities = exchange.getBody();
    // here entities are List<LinkedHashMap>
    return entities;

}

If I use,

ParameterizedTypeReference<List<AttributeInfo>>  parameterizedTypeReference = 
    new ParameterizedTypeReference<List<AttributeInfo>>(){};
    ResponseEntity<List<AttributeInfo>> exchange =
  restTemplate.exchange(uri, HttpMethod.GET, null,parameterizedTypeReference);

It works fine. But can not put in all subclasses, any other solution.

7 Answers 7

68

I worked around this using the following generic method:

public <T> List<T> exchangeAsList(String uri, ParameterizedTypeReference<List<T>> responseType) {
    return restTemplate.exchange(uri, HttpMethod.GET, null, responseType).getBody();
}

Then I could call:

List<MyDto> dtoList = this.exchangeAsList("http://my/url", new ParameterizedTypeReference<List<MyDto>>() {});

This did burden my callers with having to specify the ParameterizedTypeReference when calling, but meant that I did not have to keep a static mapping of types like in vels4j's answer 

1
  • 1
    public <T> T exchangeAsList(String uri, ParameterizedTypeReference<T> responseType) { return restTemplate.exchange(uri, HttpMethod.GET, null, responseType).getBody(); } would work the same Call as List<MyDto> dtoList = this.exchangeAsList("my/url", new ParameterizedTypeReference<>() {}); Mar 24, 2022 at 20:58
20

Using ParameterizedTypeReference for a List<Domain>, when Domain is an explicit class, that ParameterizedTypeReference works well, like this:

@Override
public List<Person> listAll() throws Exception {
    ResponseEntity<List<E>> response = restTemplate.exchange("http://example.com/person/", HttpMethod.GET, null,
            new ParameterizedTypeReference<List<Person>>() {});
    return response.getBody();
}

However, if a method listAll is used in generic flavor, that list should be parameterized itself. The best way I found for this is:

public abstract class WebServiceImpl<E> implements BaseService<E> {

    private Class<E> entityClass;

    @SuppressWarnings("unchecked")
    public WebServiceImpl() {
        this.entityClass = (Class<E>) ((ParameterizedType) getClass().getGenericSuperclass())
            .getActualTypeArguments()[0];
    }


    @Override
    public List<E> listAll() throws Exception {
        ResponseEntity<List<E>> response =  restTemplate.exchange("http://example.com/person/", HttpMethod.GET, null,
                new ParameterizedTypeReference<List<E>>() {
                    @Override
                    public Type getType() {
                        Type type = super.getType();
                        if (type instanceof ParameterizedType) {
                            Type[] responseWrapperActualTypes = { entityClass };
                            ParameterizedType responseWrapperType = new ParameterizedTypeImpl(List.class,
                                    responseWrapperActualTypes, null);
                            return responseWrapperType;
                        }
                        return type;
                    }
                });
        return response.getBody();
    }
}
3
  • 1
    This is a nice solution for getting typed collections when the entity class is only passed as a parameter, e.g. when implementing a spring-data RepositoryQuery. Thanks! Feb 12, 2019 at 9:41
  • 3
    And instead of using sun internal APIs (ParameterizedTypeImpl) you could also have a look at commons-lang3's TypeUtils. Feb 12, 2019 at 11:13
  • 1
    Your code does same thing as ParameterizedTypeReference class (from spring)
    – Mr Jedi
    Apr 22, 2021 at 7:59
15

Couldnt find a solution from Spring, hence I have done it with ParameterizedTypeReference in HashMap like

 public final static HashMap<Class,ParameterizedTypeReference> paramTypeRefMap = new HashMap() ;
 static {
    paramTypeRefMap.put(AttributeDefinition.class, new ParameterizedTypeReference<List<AttributeDefinition>>(){} );
    paramTypeRefMap.put(AttributeInfo.class, new ParameterizedTypeReference<List<AttributeInfo>>(){} );
 }

and used it

ParameterizedTypeReference parameterizedTypeReference = paramTypeRefMap.get(requiredClass);
ResponseEntity<List> exchange = restTemplate.exchange(uri, HttpMethod.POST, entity, parameterizedTypeReference);
1
  • 1
    The method exchange(String, HttpMethod, HttpEntity<?>, Class<T>, Object...) in the type RestTemplate is not applicable for the arguments (String, HttpMethod, HttpEntity<LodgingAvailabilityRequest>, ParameterizedTypeReference<List<DXARoom>) did it really worked for you ? Nov 3, 2017 at 17:49
4

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<ServicePagedResult<R>> typeRef = 
  new ParameterizedTypeReference<ServicePagedResult<R>>() {};

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

protected abstract ParameterizedTypeReference<ServicePagedResult<R>> 
getServicePagedResultTypeRef();

and then in the derived classes:

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

I could then use that in the base class like:

ResponseEntity<ServicePagedResult<R>> response = lbRestTemplate.exchange(
  uri, HttpMethod.GET, null, getServicePagedResultTypeRef(), uriVariables);
1
  • I used your solution. It seems to be the cleanest & easier one. Thanks for sharing ;) Jan 12, 2022 at 16:09
1

The easiest solution for me is to define an object MyOperationResult containing the list you expect as field and use restTemplate.getForObject to get this result.

1

In case someone need a Kotlin solution, you can do:

val responseType = object : ParameterizedTypeReference<Map<String, Any?>>() {}         
val request = HttpEntity<Any?>(data)
val response = restTemplate.exchange(url, HttpMethod.POST, request, responseType)
val responseMap = response?.body as Map<String, Any>
1

You can use TypeUtil from apache's commons-lang3 to build the generic Type to pass to exchange:

 public List<T> restFindAll() {

    RestTemplate restTemplate = RestClient.build().restTemplate();
    String uri= BASE_URI +"/"+ getPath();

    ResponseEntity<List<T>> exchange = restTemplate.exchange(uri, HttpMethod.GET, null, ParameterizedTypeReference.forType(TypeUtils.parameterize(List.class, clazz)));
    List<T> entities = exchange.getBody();
    return entities;

}
2
  • Where does TypeUtils come from? Can you provide the fully qualified class Name
    – Sniper
    Apr 22, 2022 at 11:25
  • 1
    @Sniper here you go :) Apr 24, 2022 at 4:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.