Configuring the RestTemplate
All the packages/libraries for RestTemplate are present in spring-boot-starter-web which comes under org.springframework.web
XML
< dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-web</ artifactId > </ dependency > |
You must define @Bean of RestTemplate in Spring Boot Configuration.
Java
// Bean Configuration for RestTemplate @Bean public RestTemplate restTemplate(){ return new RestTemplate(); } |
Note: While declaring the RestTemplate @Bean in separate config class Its important to annotate the class with @Configuration, then only @Bean gets recognised by Spring boot Application.
Spring Boot – Rest Template
RestTemplate is a powerful synchronous client for handling HTTP communication in Spring Boot applications. It internally uses an HTTP client library i.e. java.net.HttpURLConnection, simplifying the process of making RESTful requests to external services and APIs, including convenience, along with integration, and flexibility for various HTTP communication. Overall in this article, we will see how to invoke external APIs using methods provided by RestTemplate in your Spring Boot Application. RestTemplate is a well-known rest client in the Spring framework to performs synchronous HTTP requests.