Skip to content

Access HAL+JSON resources using RestTemplate from RestTemplateBuilder #1049

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
mkhl opened this issue Aug 11, 2019 · 4 comments
Open

Access HAL+JSON resources using RestTemplate from RestTemplateBuilder #1049

mkhl opened this issue Aug 11, 2019 · 4 comments

Comments

@mkhl
Copy link

mkhl commented Aug 11, 2019

I'm using Spring 5.1.8, Spring Boot 2.1.6, and Spring HATEOAS 0.25.1.

I want to construct the RestTemplate in the using @Component by injecting RestTemplateBuilder.
This seems to be the recommended way and allows different components to define their own error handling instead of sharing it across components.

ConverterRegisteringBeanPostProcessor configures any RestTemplate bean in the application context (using ConverterRegisteringWebMvcConfigurer), but it doesn't affect RestTemplateBuilder.
For that I have to define the following RestTemplateCustomizer (based on this comment):

@Configuration
@EnableHypermediaSupport(type = HAL)
class HalConfiguration {
    @Bean
    RestTemplateCustomizer halRestTemplateCustomizer(ConverterRegisteringWebMvcConfigurer configurer) {
        return template -> configurer.extendMessageConverters(template.getMessageConverters());
    }
}

It seems to me that (something like) this would make sense to include in spring-hateoas.
Alternatively, it would be nice if there were an easier way to access or inject hypermedia-enabled HttpMessageConverters.

@gregturn
Copy link
Contributor

@mkhl
Copy link
Author

mkhl commented Aug 12, 2019

I hadn't, thank you for the link. I'm not sure I see the relevance to RestTemplateBuilder and RestTemplateCustomizer, did you maybe mean to add this to #1050 or #1051?

@rustige
Copy link

rustige commented Nov 12, 2019

I am on the same project as @mkhl. Now we use for 1.0.1-RELEASE following workaround to register the HAL converter:

@Configuration
public class RestTemplateConfig {

    @Bean
    RestTemplateCustomizer halRestTemplateCustomizer(List<WebMvcConfigurer> configurers) {
        WebMvcConfigurer webMvcConfigurer = configurers.stream()
                                                       .filter(configurer -> configurer.getClass().getCanonicalName().contains("org.springframework.hateoas"))
                                                       .findFirst()
                                                       .get();
        return template -> webMvcConfigurer.extendMessageConverters(template.getMessageConverters());
    }
}

As WebMvcConfigurer is package-private we cannot access it directly.

@mkhl
Copy link
Author

mkhl commented Nov 12, 2019

(He means org.springframework.hateoas.config.WebMvcHateoasConfiguration.HypermediaWebMvcConfigurer, which is package-private and is the WebMvcConfigurer we're finding in that code snippet.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants