Last active
October 26, 2019 07:17
-
-
Save donchan922/16aa7470c2d0cd86eb68853a6b2b0702 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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