Skip to content

Instantly share code, notes, and snippets.

@Kallaste
Forked from ripla/RestConfiguration.java
Created April 7, 2019 08:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kallaste/496c247400305fbb5d43a24eae234404 to your computer and use it in GitHub Desktop.
Save Kallaste/496c247400305fbb5d43a24eae234404 to your computer and use it in GitHub Desktop.
The different ways of accessing a REST HATEOAS resource created with Spring Data. Using a Spring RestTemplate.
import java.util.Arrays;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.hateoas.MediaTypes;
import org.springframework.hateoas.hal.Jackson2HalModule;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
@Configuration
public class RestConfiguration {
@Bean
@LoadBalanced
public RestTemplate restTemplate(RestTemplateBuilder builder) {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,
false);
mapper.registerModule(new Jackson2HalModule());
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setSupportedMediaTypes(Arrays.asList(MediaTypes.HAL_JSON));
converter.setObjectMapper(mapper);
return builder.messageConverters(converter).build();
}
}
import org.springframework.hateoas.Resources;
public class StudentResources extends Resources<Student> {
}
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<Student> getStudents(int offset, int limit) {
Map<String, Integer> params = new HashMap<>();
params.put("page", offset / limit);
params.put("size", limit);
// Using external class
final ResponseEntity<StudentResources> studentResponse = template
.getForEntity(URL, StudentResources.class, params);
// Using instantiated ParametrizedTypeReference Resources
final ResponseEntity<Resources<Student>> studentResponse = template
.exchange(URL, HttpMethod.GET, null,
new ParameterizedTypeReference<Resources<Student>>() {
}, params);
// Using instantiated ParametrizedTypeReference Resources
final ResponseEntity<PagedResources<Student>> studentResponse = template
.exchange(URL, HttpMethod.GET, null,
new ParameterizedTypeReference<PagedResources<Student>>() {
}, params);
// Does not work for some reason, ends up with empty Resources inside Resources
// final ResponseEntity<Resources<Resource<Student>>> studentResponse = template
// .exchange(URL, HttpMethod.GET, null,
// new TypeReferences.ResourcesType<Resource<Student>>() {
// }, params);
// Using provided PagedResources type class, note the required {}
// This is used for return
final ResponseEntity<PagedResources<Resource<Student>>> studentResponse =
template
.exchange(URL, HttpMethod.GET, null,
new TypeReferences.PagedResourcesType<Resource<Student>>(){},
params);
return studentResponse.getBody().getContent().stream()
.map(Resource::getContent);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment