Issue
I have developed a spring application with rest controller which will have only one GET method. Can i configure the Spring MVC application so that when ever i run the application, the controller or any method should be called automatically with out hitting the api from browser(Just like stand alone application)
Solution
You can create a bean that implements org.springframework.boot.CommandLineRunner
interface.
E.g.
@Component
public class MyRunner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
// Do stuff here at startup
}
}
As this is a regular Spring component, you can use field or constructor injection to inject other Spring components.
Answered By - Wim Deblauwe
Answer Checked By - Mary Flores (JavaFixing Volunteer)