#1223 - Provide API to configure RestTemplate with hypermedia.
Create a HypermediaRestTemplateConfigurer bean registered as a Spring HATEOAS bean. Update reference documentation to show how to use it directly and with Spring Boot via RestTemplateCustomizer.
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
package org.springframework.hateoas.config;
|
||||
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
/**
|
||||
* Assembles hypermedia-based message converters and applies them to an existing {@link RestTemplate}.
|
||||
*
|
||||
* @author Greg Turnquist
|
||||
* @since 1.1
|
||||
*/
|
||||
public class HypermediaRestTemplateConfigurer {
|
||||
|
||||
private final WebConverters converters;
|
||||
|
||||
HypermediaRestTemplateConfigurer(WebConverters converters) {
|
||||
this.converters = converters;
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert hypermedia-aware message converters in front of any other existing message converters.
|
||||
*
|
||||
* @param template
|
||||
* @return
|
||||
*/
|
||||
public RestTemplate registerHypermediaTypes(RestTemplate template) {
|
||||
|
||||
template.setMessageConverters(converters.and(template.getMessageConverters()));
|
||||
return template;
|
||||
}
|
||||
}
|
||||
@@ -16,9 +16,7 @@
|
||||
package org.springframework.hateoas.config;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
@@ -36,8 +34,13 @@ class RestTemplateHateoasConfiguration {
|
||||
|
||||
@Bean
|
||||
static HypermediaRestTemplateBeanPostProcessor hypermediaRestTemplateBeanPostProcessor(
|
||||
ObjectProvider<WebConverters> converters) {
|
||||
return new HypermediaRestTemplateBeanPostProcessor(converters);
|
||||
HypermediaRestTemplateConfigurer configurer) {
|
||||
return new HypermediaRestTemplateBeanPostProcessor(configurer);
|
||||
}
|
||||
|
||||
@Bean
|
||||
HypermediaRestTemplateConfigurer hypermediaRestTemplateConfigurer(WebConverters converters) {
|
||||
return new HypermediaRestTemplateConfigurer(converters);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -50,7 +53,7 @@ class RestTemplateHateoasConfiguration {
|
||||
@RequiredArgsConstructor
|
||||
static class HypermediaRestTemplateBeanPostProcessor implements BeanPostProcessor {
|
||||
|
||||
private final ObjectProvider<WebConverters> converters;
|
||||
private final HypermediaRestTemplateConfigurer configurer;
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
@@ -64,10 +67,7 @@ class RestTemplateHateoasConfiguration {
|
||||
return bean;
|
||||
}
|
||||
|
||||
RestTemplate template = (RestTemplate) bean;
|
||||
template.setMessageConverters(converters.getObject().and(template.getMessageConverters()));
|
||||
|
||||
return template;
|
||||
return this.configurer.registerHypermediaTypes((RestTemplate) bean);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,18 +15,17 @@
|
||||
*/
|
||||
package org.springframework.hateoas.config;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.springframework.hateoas.RepresentationModel;
|
||||
import org.springframework.hateoas.server.mvc.TypeConstrainedMappingJackson2HttpMessageConverter;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
import org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Value type to handle registration of hypermedia related {@link HttpMessageConverter}s.
|
||||
|
||||
Reference in New Issue
Block a user