java-how to solve Field restTemplate required a bean of type 'org.springframework.web.client.RestTemplate' that could not be found.

Problem

When we use RestTemplate in java projects, sometimes, we get this error:

 .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.2.RELEASE)
2020-12-10 08:15:37.698  INFO 1 --- [           main] b.c.PropertySourceBootstrapConfiguration : Located property source: [BootstrapPropertySource {name='bootstrapProperties-configmap.config-app7.ns-bswen'}]
2020-12-10 08:15:37.724  INFO 1 --- [           main] com.bswen.app7.Main                      : The following profiles are active: kubernetes,dev
2020-12-10 08:15:38.602  WARN 1 --- [           main] o.s.boot.actuate.endpoint.EndpointId     : Endpoint ID 'service-registry' contains invalid characters, please migrate to a valid format.
2020-12-10 08:15:38.762  INFO 1 --- [           main] o.s.cloud.context.scope.GenericScope     : BeanFactory id=40fc3e63-6feb-34fb-b3b4-fa21a396b02e
2020-12-10 08:15:39.277  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8082 (http)
2020-12-10 08:15:39.296  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-12-10 08:15:39.298  INFO 1 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.37]
2020-12-10 08:15:39.509  INFO 1 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-12-10 08:15:39.509  INFO 1 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1762 ms
2020-12-10 08:15:39.857  WARN 1 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'greetingController': Unsatisfied dependency expressed through field 'restTemplate'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.web.client.RestTemplate' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
2020-12-10 08:15:39.861  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2020-12-10 08:15:39.878  INFO 1 --- [           main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-12-10 08:15:40.009 ERROR 1 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   :
***************************
APPLICATION FAILED TO START
***************************
Description:
Field restTemplate in com.bswen.app7.GreetingController required a bean of type 'org.springframework.web.client.RestTemplate' that could not be found.
The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'org.springframework.web.client.RestTemplate' in your configuration.

The core exception is:

Field restTemplate in com.bswen.app7.GreetingController required a bean of type 'org.springframework.web.client.RestTemplate' that could not be found.

Environment

  • java 1.8
  • Spring Boot 2.3
  • Intellij IDEA

Reason

We must provide a RestTemplate bean to spring boot framework.

Solution

We should define a bean of type ‘org.springframework.web.client.RestTemplate’ in our configuration as follows:

@Configuration
public class MyConfig {

    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder builder) {
        // Do any additional configuration here
        return builder.build();
    }
}