SpringBoot b2b2c 多用戶商城系統 (十六)用restTemplate消費服務
阿新 • • 發佈:2019-01-14
.class restfu ESS targe 打印 pla str blank 分布
構架工程
創建一個springboot工程,去消費RESTFUL的服務。這個服務是 http:///gturnquist-quoters.cfapps.io/api/random ,它會隨機返回Json字符串。
在Spring項目中,它提供了一個非常簡便的類,叫RestTemplate,它可以很簡便的消費服務。
消費服務
通過RestTemplate消費服務,需要先context中註冊一個RestTemplate bean。代碼如下:
@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()); }; }
運行程序,控制臺打印:
{ “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.” } }
架構代碼如下 :
完整項目的源碼來源 Spring Cloud大型企業分布式微服務雲構建的B2B2C電子商務平臺源碼請加企鵝求求: 一零叁八七七四六貳六
SpringBoot b2b2c 多用戶商城系統 (十六)用restTemplate消費服務