springboot发送http请求
生活随笔
收集整理的這篇文章主要介紹了
springboot发送http请求
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
springboot中實現(xiàn)http請求調(diào)用api
創(chuàng)建發(fā)送http請求service層
import?org.springframework.http.*; import?org.springframework.stereotype.Service; import?org.springframework.util.MultiValueMap; import?org.springframework.web.client.RestTemplate;/***?@Author?馮戰(zhàn)魁*?@Date?2018/1/23?下午5:43*/ @Service public?class?HttpClient?{public?String?client(String?url,?HttpMethod?method,?MultiValueMap<String,?String>?params){RestTemplate?client?=?new?RestTemplate();HttpHeaders?headers?=?new?HttpHeaders();//??請勿輕易改變此提交方式,大部分的情況下,提交方式都是表單提交headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);HttpEntity<MultiValueMap<String,?String>>?requestEntity?=?new?HttpEntity<MultiValueMap<String,?String>>(params,?headers);//??執(zhí)行HTTP請求ResponseEntity<String>?response?=?client.exchange(url,?HttpMethod.POST,?requestEntity,?String.class);return?response.getBody();} }添加本地測試url localhost:8080/hello
訪問localhost:8080/hello查看調(diào)用結(jié)果
curl http://localhost:8080/hello
本文轉(zhuǎn)自 無心低語 51CTO博客,原文鏈接:http://blog.51cto.com/fengzhankui/2064327,如需轉(zhuǎn)載請自行聯(lián)系原作者
總結(jié)
以上是生活随笔為你收集整理的springboot发送http请求的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 异步陷阱之IO篇
- 下一篇: python_day2_数据格式