Skip to content

Instantly share code, notes, and snippets.

@donchan922
Last active October 26, 2019 07:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save donchan922/16aa7470c2d0cd86eb68853a6b2b0702 to your computer and use it in GitHub Desktop.
Save donchan922/16aa7470c2d0cd86eb68853a6b2b0702 to your computer and use it in GitHub Desktop.
@SpringBootApplication
public class DemoApplication implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
// RestTemplateを定義する
private final RestTemplate restTemplate;
// RestTemplateをコンストラクタインジェクションする
public DemoApplication(RestTemplateBuilder restTemplateBuilder) {
// RestTemplateインスタンスを生成する
this.restTemplate = restTemplateBuilder.build();
}
@Override
public void run(String... args) {
// APIにGETでリクエストする
// 第一引数:URL、第二引数:レスポンスの型
Quote response = restTemplate.getForObject("https://gturnquist-quoters.cfapps.io/api/random", Quote.class);
System.out.println(response.toString()); // Quote{type='success', value=Value{id=1, quote='Working with Spring Boot is like pair-programming with the Spring developers.'}}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment