'Could not autowire. No beans of 'RestTemplateBuilder' type found

I have a Java-Spring LIBRARY (NOT an application) which sends notifications via phone numbers. I'm using a Rest template to send a POST request. But instead of creating a new object of RestTemplate, I would want to use RestTemplateConfiguration @Configuration to do so.

  1. IntelliJ version - 2020.2
  2. Spring - 2.3.3.RELEASE
  3. Java - 11

1st issue - When I'm trying to create a RestTemplateConfiguration class, I get the error - Could not autowire. No beans of 'RestTemplateBuilder' type found.

@Configuration
public class RestTemplateConfiguration {
    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder builder) {
        return builder.build();
    }
}

2nd issue If I create a new object of RestTemplateBuilder, it doesn't give the error, but this is not how I want this class to behave. Also, this doesn't seem to work when in the class (mentioned below) SamplePhoneNumbers.java, I try to use RestTemplate restTemplate. It is coming as null.

@Configuration
public class RestTemplateConfiguration {
    @Bean
    public RestTemplate restTemplate() {
        RestTemplateBuilder builder = new RestTemplateBuilder;
        return builder.build();
    }
}

Here is the class which sends notifications and where I am trying to use the Rest Template.

@Slf4j
public class SamplePhoneNumbers implements SampleClassStratergy {

    RestTemplate restTemplate;
    private static SamplePhoneNumbers samplePhoneNumbers = null;
    private String phoneNumbersNotificationServiceURL = Enums.NotificationURL.enum_value + Enums.PhoneNumberApi.enum_value;

    SamplePhoneNumbers() {
        this.restTemplate = new RestTemplate();
    }

    public static SamplePhoneNumbers getInstance() {
        if(samplePhoneNumbers ==null) {
            samplePhoneNumbers = new SamplePhoneNumbers();
        }
        return samplePhoneNumbers;
    }

    @Async
    public void sendNotification(String title, String message, List<String> listOfPhoneNumbers) {
        SmsMessage smsMessage= new SmsMessage(title, message, listOfPhoneNumbers, Collections.emptyList(), Collections.emptyList());

        try {
            HttpEntity<SmsMessage> newRequest = new HttpEntity<>(smsMessage);
            restTemplate.postForObject(phoneNumbersNotificationServiceURL, newRequest, String.class);
            log.info("Notification sent via phone number.");
        } catch (Exception e) {
            log.error(e.getMessage());
        }
    }
}

3rd issue: Is there any way I can get rid of getInstance() method so I don't have to handle Singleton logic? If I can create the class as a bean, that should work I guess, but I'm not sure how that can be achieved in this case.

Since this is a Library, it doesn't have a main method and no @SpringBootapplicationContext.

Can someone please assist me with the solution?



Solution 1:[1]

I also got the similar error while running Junit test case and resolved it by adding a bean of -

@Bean
    public RestTemplateBuilder restTemplateBuilder() {

        return new RestTemplateBuilder();
    }

Solution 2:[2]

Do you want invoke your Java-Spring LIBRARY in a spring boot application? If so, you should add @ComponentScan in spring boot application main method as below.

pacakge com.prod

// for instance, Java-Spring LIRARY package is com.third
@SpringBootApplication
@ComponentScan(basePackages={"com.prod","com.third"})
public class Application {

Then you should do as below:


@Configuration
public class RestTemplateConfiguration {
    
    @Bean(name="myRestTemplate")
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

@Slf4j
@Component
public class SamplePhoneNumbers implements SampleClassStratergy {

    @Autowired
    @Qualifier("myRestTemplate")
    RestTemplate restTemplate;

        @Async
    public void sendNotification(String title, String message, List<String> listOfPhoneNumbers) {
        SmsMessage smsMessage= new SmsMessage(title, message, listOfPhoneNumbers, Collections.emptyList(), Collections.emptyList());

        try {
            HttpEntity<SmsMessage> newRequest = new HttpEntity<>(smsMessage);
            restTemplate.postForObject(phoneNumbersNotificationServiceURL, newRequest, String.class);
            log.info("Notification sent via phone number.");
        } catch (Exception e) {
            log.error(e.getMessage());
        }
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Slava Rozhnev
Solution 2 jacky-neo