Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
@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) {
// REST 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
You can’t perform that action at this time.