什么是在spring-boot启动时调用服务的最佳方法?

时间:2016-06-03 19:46:58

标签: java spring spring-boot spring-ioc

我有一个spring boot应用程序,我需要在启动时调用服务(一个休息终点)。

4 个答案:

答案 0 :(得分:2)

CommandLineRunner

RestTemplate restTemplate = new RestTemplate();
String result = restTemplate.getForObject("http://www.example.com/api/resource", String.class);

您可以使用这个方便的界面在应用程序启动时执行您想要的任何任务。

要调用REST端点,您可以使用 RestTemplate

{{1}}

如果您使用与JSON响应匹配的字段构建POJO,RestTemplate将在Jackson的帮助下自动映射它们。有关更多详细信息,请参阅文档。

答案 1 :(得分:1)

我建议您查看@PostConstruct注释。

答案 2 :(得分:0)

我使用和实施ApplicationRunner

答案 3 :(得分:0)