import java.util.HashMap; import java.util.Map; import java.util.stream.Stream; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.ParameterizedTypeReference; import org.springframework.hateoas.PagedResources; import org.springframework.hateoas.Resource; import org.springframework.hateoas.Resources; import org.springframework.hateoas.mvc.TypeReferences; import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component; import org.springframework.web.client.RestTemplate; @Component public class StudentsClient { private static final String URL = "http://students-service/students?page={page}&size={size}"; @Autowired RestTemplate template; public Stream getStudents(int offset, int limit) { Map params = new HashMap<>(); params.put("page", offset / limit); params.put("size", limit); // Using external class final ResponseEntity studentResponse = template .getForEntity(URL, StudentResources.class, params); // Using instantiated ParametrizedTypeReference Resources final ResponseEntity> studentResponse = template .exchange(URL, HttpMethod.GET, null, new ParameterizedTypeReference>() { }, params); // Using instantiated ParametrizedTypeReference Resources final ResponseEntity> studentResponse = template .exchange(URL, HttpMethod.GET, null, new ParameterizedTypeReference>() { }, params); // Does not work for some reason, ends up with empty Resources inside Resources // final ResponseEntity>> studentResponse = template // .exchange(URL, HttpMethod.GET, null, // new TypeReferences.ResourcesType>() { // }, params); // Using provided PagedResources type class, note the required {} // This is used for return final ResponseEntity>> studentResponse = template .exchange(URL, HttpMethod.GET, null, new TypeReferences.PagedResourcesType>(){}, params); return studentResponse.getBody().getContent().stream() .map(Resource::getContent); } }