0

I have a Spring MVC application. Below is one class that has RestTemplate and calling external service, it's working fine.

We migrated Spring MVC (war) to Spring Boot 1.5.7 (jar). With migration, below is giving NullPointerException for restTemplate. I have added a print statement and restTemplate is null. I have added @Bean with getRestTemplate(), but that's still giving same error.

@Service
Class Impl{

   @Autowired
   private RestTemplate restTemplate;

   public RestTemplate getRestTemplate() {
       return restTemplate;
   }

   public void setRestTemplate(final RestTemplate restTemplate) {
       this.restTemplate = restTemplate;
   }

} 

How do I fix this ? Why is it working with Spring MVC and not with Spring Boot?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Sachin
  • 29
  • 2
  • Can you share your `pom.xml`? Thanks. – João Dias Aug 25 '21 at 15:51
  • An `@Autowired` field cannot be `null` if a dependency cannot be fulfilled your application wouldn't start. If it is null you are creating an instance of your `Impl` yourself. See https://deinum.biz/2020-07-03-Autowired-Field-Null/ – M. Deinum Aug 25 '21 at 15:51
  • My pom.xml is here- org.springframework.boot spring-boot-starter-web I do have other non spring dependencies as well in pom.xml – Sachin Aug 26 '21 at 08:07

0 Answers0