java版spring cloud+spring boot+redis社交電子商務平臺 (十六)用restTemplate消費服務

構架工程

電子商務社交平臺源碼請加企鵝求求:叄五叄六貳四柒貳五九。spring

建立一個springboot工程,去消費RESTFUL的服務。這個服務是 http:///gturnquist-quoters.cfapps.io/api/random ,它會隨機返回Json字符串。
在Spring項目中,它提供了一個很是簡便的類,叫RestTemplate,它能夠很簡便的消費服務。api

消費服務

經過RestTemplate消費服務,須要先context中註冊一個RestTemplate bean。代碼以下:springboot

複製代碼
@Bean
    public RestTemplate restTemplate(RestTemplateBuilder builder) {
        return builder.build();
    }
 
    @Bean
    public CommandLineRunner run(RestTemplate restTemplate) throws Exception {
        return args -> {
            String quote = restTemplate.getForObject(
                    "http://gturnquist-quoters.cfapps.io/api/random", String.class);
            log.info(quote.toString());
        };
    }複製代碼
複製代碼

運行程序,控制檯打印:bash

複製代碼
{
「type」: 「success」,
「value」: {
「id」: 6,
「quote」: 「It embraces convention over configuration, providing an experience on par with frameworks that excel at early stage development, such as Ruby on Rails.」
}
}複製代碼
複製代碼

電子商務社交平臺源碼請加企鵝求求:叄五叄六貳四柒貳五九

相關文章
相關標籤/搜索