filmov
tv
Spring Boot Consume RESTful Web Service with RESTTemplateBuilder
![preview_player](https://i.ytimg.com/vi/MnhCW0uKDCU/maxresdefault.jpg)
Показать описание
How to easily consume a RESTful API Web Service with Java, Spring Boot and the new RestTemplateBuilder.
I start off by creating a RESTful web service with Spring’s RestTemplateBuilder. With Spring Boot 1.4.0+, this class will be automatically instantiated and will be available for dependency injection.
With this builder, we can set the REST client’s basic authentication, connect timeout, read timeout, http request factory, converters, error handlers and many more settings. This is a great convenience as we no longer need to manually do it ourselves.
We then create a class to hold our Rest Client Properties, this bean is annotated with Spring’s @ConfigurationProperties with a prefix specified. As we have spring-boot-configuration-processor on the classpath, we now have intellisense and code completion in our property files and are now type safe when defining properties. No more mistakes! These properties are then used to set the values inside the RestTemplateBuilder. We aim to externalise all configuration, especially if it can change per environment.
I then go ahead and use the Spring CommandLineRunner to execute the run method and call the REST service.
The rest template will use the default Simple Spring HTTP Request factory as we do not have any of the predefined REQUEST_FACTORY_CANDIDATES on our classpath. I then add Http Components HttpClient and show that the request factory is now the springframework httpComponentsClientHttpRequestFactory.
I hope you enjoyed this tutorial and start using RestTemplateBuilder :)
Don’t forget to subscribe for more great videos!
Till next time!
Philip
I start off by creating a RESTful web service with Spring’s RestTemplateBuilder. With Spring Boot 1.4.0+, this class will be automatically instantiated and will be available for dependency injection.
With this builder, we can set the REST client’s basic authentication, connect timeout, read timeout, http request factory, converters, error handlers and many more settings. This is a great convenience as we no longer need to manually do it ourselves.
We then create a class to hold our Rest Client Properties, this bean is annotated with Spring’s @ConfigurationProperties with a prefix specified. As we have spring-boot-configuration-processor on the classpath, we now have intellisense and code completion in our property files and are now type safe when defining properties. No more mistakes! These properties are then used to set the values inside the RestTemplateBuilder. We aim to externalise all configuration, especially if it can change per environment.
I then go ahead and use the Spring CommandLineRunner to execute the run method and call the REST service.
The rest template will use the default Simple Spring HTTP Request factory as we do not have any of the predefined REQUEST_FACTORY_CANDIDATES on our classpath. I then add Http Components HttpClient and show that the request factory is now the springframework httpComponentsClientHttpRequestFactory.
I hope you enjoyed this tutorial and start using RestTemplateBuilder :)
Don’t forget to subscribe for more great videos!
Till next time!
Philip