2

I have a spring boot application and i need to invoke a service(a rest end point)on start up.

luboskrnac
  • 23,973
  • 10
  • 81
  • 92
Diva
  • 151
  • 1
  • 3
  • 9

4 Answers4

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

I suggest to take a look at @PostConstruct annotation.

luboskrnac
  • 23,973
  • 10
  • 81
  • 92
2

i'd use and implementation of ApplicationRunner

Akli REGUIG
  • 552
  • 4
  • 13
2

You could also hook your app on the ApplicationReadyEvent or other events triggered by the Spring :

http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-spring-application.html#boot-features-application-events-and-listeners

jebeaudet
  • 1,533
  • 16
  • 15