Skip to content

How to resolve Field restTemplate required a bean of type 'org.springframework.web.client.RestTemplate' that could not be found

Problem

When using RestTemplate in Java projects, you might encounter the following error:

Terminal window
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: 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:

Terminal window
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

The error occurs because Spring Boot cannot find a RestTemplate bean to inject into your application. You need to explicitly define a RestTemplate bean in your configuration.

Solution

To resolve this issue, define a RestTemplate bean in your configuration as follows:

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

Summary

In this post, we explored how to resolve the “RestTemplate bean not found” error in Spring Boot. The key takeaway is to ensure that you define a RestTemplate bean in your configuration. This allows Spring Boot to inject the bean wherever it is required, preventing the application from failing at startup. By following the solution provided, you can easily fix this issue and ensure your application runs smoothly.

Final Words + More Resources

My intention with this article was to help others who might be considering solving such a problem. So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by email: Email me

Here are also the most important links from this article along with some further resources that will help you in this scope:

Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!