I have a spring boot application and i need to invoke a service(a rest end point)on start up.
Asked
Active
Viewed 4,192 times
4 Answers
3
CommandLineRunner
@Component
public class MyBean implements CommandLineRunner {
public void run(String... args) {
// Do something...
}
}
You can perform any task you'd like at Application startup with this handy interface.
To call a REST endpoint you can use RestTemplate
RestTemplate restTemplate = new RestTemplate();
String result = restTemplate.getForObject("http://www.example.com/api/resource", String.class);
If you build a POJO with fields that match the JSON response, the RestTemplate will automatically map them with the help of Jackson. See the docs for more detail.

Kyle Anderson
- 6,801
- 1
- 29
- 41
2
You could also hook your app on the ApplicationReadyEvent
or other events triggered by the Spring :

jebeaudet
- 1,533
- 16
- 15