日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

springboot发送http请求

發布時間:2025/7/14 编程问答 61 豆豆
生活随笔 收集整理的這篇文章主要介紹了 springboot发送http请求 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

springboot中實現http請求調用api

  • 創建發送http請求service層

    import?org.springframework.http.*; import?org.springframework.stereotype.Service; import?org.springframework.util.MultiValueMap; import?org.springframework.web.client.RestTemplate;/***?@Author?馮戰魁*?@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);//??執行HTTP請求ResponseEntity<String>?response?=?client.exchange(url,?HttpMethod.POST,?requestEntity,?String.class);return?response.getBody();} }
  • 添加本地測試url localhost:8080/hello

    import?com.example.demo.service.HttpClient; import?org.springframework.beans.factory.annotation.Autowired; import?org.springframework.http.*; import?org.springframework.util.LinkedMultiValueMap; import?org.springframework.util.MultiValueMap; import?org.springframework.web.bind.annotation.RequestMapping; import?org.springframework.web.bind.annotation.RestController;/***?@Author?馮戰魁*?@Date?2018/1/8?上午11:17*/ @RestController public?class?HelloController?{@AutowiredHttpClient?httpClient;@RequestMapping("/hello")public?String?hello(){//api?url地址String?url?=?"http://xxxx";//post請求HttpMethod?method?=HttpMethod.POST;//?封裝參數,千萬不要替換為Map與HashMap,否則參數無法傳遞MultiValueMap<String,?String>?params=?new?LinkedMultiValueMap<String,?String>();params.add("access_token",?"xxxxx");//發送http請求并返回結果return?httpClient.client(url,method,params);} }
  • 訪問localhost:8080/hello查看調用結果

    curl http://localhost:8080/hello










  • 本文轉自 無心低語 51CTO博客,原文鏈接:http://blog.51cto.com/fengzhankui/2064327,如需轉載請自行聯系原作者

    總結

    以上是生活随笔為你收集整理的springboot发送http请求的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。