Springboot?HTTP如何調(diào)用其他服務(wù)
更新時間:2022年01月28日 10:09:44 作者:靜忻寒
這篇文章主要介紹了Springboot?HTTP如何調(diào)用其他服務(wù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
HTTP如何調(diào)用其他服務(wù)
1.GET請求
1.1Client代碼
import com.alibaba.fastjson.JSON; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; import org.springframework.web.util.UriComponentsBuilder; import java.net.URI; import java.util.HashMap; import java.util.Map; ? @Service public class UserInfoClient { ? ? public String getUserTotalAmount(){ ? ? ? ? Map<String,String> map=new HashMap<String,String>(); ? ? ? ? map.put("name","123"); ? ? ? ? map.put("password","123"); ? ? ? ? URI uri = UriComponentsBuilder.fromHttpUrl("http://localhost:8081/spring/test") ? ? ? ? ? ? ? ? .queryParam("jsonString",JSON.toJSONString(map)) ? ? ? ? ? ? ? ? .queryParam("token","12122222111") ? ? ? ? ? ? ? ? .build().encode().toUri(); ? ? ? ? RestTemplate restTemplate=new RestTemplate(); ? ? ? ? String data=restTemplate.getForObject(uri,String.class); ? ? ? ? System.out.println(data); ? ? ? ? return null; ? ? } ? ? public static void main(String[] args){ ? ? ? ? UserInfoClient c=new UserInfoClient(); ? ? ? ? c.getUserTotalAmount(); ? ? } }
1.2 Service 代碼
import org.springframework.web.bind.annotation.*;? @RestController @RequestMapping(value = "/spring") public class Test { ? ? @RequestMapping(value = "/test",method = RequestMethod.GET) ? ? public String testSpringBoot(@RequestParam String jsonString,@RequestParam String token){ ? ? ? ? System.out.println(jsonString); ? ? ? ? System.out.println(token); ? ? ? ? /* ? ? ? ? ?*邏輯處理 ? ? ? ? ?*/ ? ? ? ? return "Spring Boot 測試成功!"; ? ? } }
2.POST請求
2.1Client代碼
import com.alibaba.fastjson.JSON; import org.springframework.http.*; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; import java.util.HashMap; import java.util.Map; ? @Service public class UserInfoClient {? ? ? public String getUserTotalAmount(){ ? ? ? ? Map<String,String> map=new HashMap<String,String>(); ? ? ? ? map.put("name","123"); ? ? ? ? map.put("password","123"); ? ? ? ? String url="http://localhost:8081/spring/test"; ? ? ? ? //設(shè)置請求頭信息 ? ? ? ? HttpHeaders headers = new HttpHeaders(); ? ? ? ? MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8"); ? ? ? ? headers.setContentType(type); ? ? ? ? headers.add("Accept", MediaType.APPLICATION_JSON.toString()); ? ? ? ? //設(shè)置body部分 ? ? ? ? HttpEntity<String> entity = new HttpEntity<String>(JSON.toJSONString(map),headers); ? ? ? ? RestTemplate restTemplate=new RestTemplate(); ? ? ? ? ResponseEntity<String> result = restTemplate.exchange(url, HttpMethod.POST, entity, String.class); ? ? ? ? System.out.println(result.getBody()); ? ? ? ? return null; ? ? } ? ? public static void main(String[] args){ ? ? ? ? UserInfoClient c=new UserInfoClient(); ? ? ? ? c.getUserTotalAmount(); ? ? } }
2.2 Service代碼
import org.springframework.web.bind.annotation.*; @RestController @RequestMapping(value = "/spring") public class Test { @RequestMapping(value = "/test",method = RequestMethod.POST) public String testSpringBoot(@RequestBody UserBean userBean){ System.out.println(userBean); /* *邏輯處理 */ return "Spring Boot 測試成功!"; } }
springboot請求其他服務(wù)器的http接口
使用Get方式,攜帶headers請求數(shù)據(jù)
//注入 @Autowired private RestTemplate restTemplate; @RequestMapping("/FaceInfo") @ResponseBody public Object ? faceInfo(String startTime,String endTime,Integer size ){ ? ? String apiURL = "http://www.05un.cn/wspp/GceGroups"; ? ? HttpHeaders headers = new HttpHeaders(); ? ?headers.add("userId","38"); ? ? // headers.set("userId","38"); ? ? headers.setContentType(MediaType.APPLICATION_JSON); ? ? Map<String, Object> requestParam = new HashMap<>(); ? ? HttpEntity<Map<String, Object>> request = new HttpEntity<Map<String, Object>>(requestParam, headers); ? ? ? ? ResponseEntity<String> entity2 = restTemplate.exchange(apiURL, HttpMethod.GET, request, String.class); ? ? String body = entity2.getBody(); ? ? return body; }
使用Post方式,攜帶body請求數(shù)據(jù)
//注入 @Autowired private RestTemplate restTemplate; @RequestMapping("/FaceInfo") @ResponseBody public Object ? faceInfo(String startTime,String endTime,Integer size ){ ? ? String apiURL = "http://www.0531yun.cn/wsjc/app/Login"; ? ? HttpHeaders headers = new HttpHeaders(); ? ? headers.setContentType(MediaType.APPLICATION_JSON); ? ? Map<String, Object> requestParam = new HashMap<>(); ? ? requestParam.put("loginName", "jnr"); ? ? requestParam.put("password", "jn"); ? ? HttpEntity<Map<String, Object>> request = new HttpEntity<Map<String, Object>>(requestParam, headers); ? ? String s=request.toString(); ? ? ResponseEntity<String> entity2 = restTemplate.exchange(apiURL, HttpMethod.POST, request, String.class); ? ? String body = entity2.getBody(); ? ? return body; }
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
java.lang.NullPointerException 如何處理空指針異常的實現(xiàn)
這篇文章主要介紹了java.lang.NullPointerException 如何處理空指針異常的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12Java Swing中的下拉式菜單(menu)、彈出式菜單(JPopupMenu)、選項卡窗體(JTabbedPane)
這篇文章主要介紹了Java Swing中的下拉式菜單(menu)、彈出式菜單(JPopupMenu)、選項卡窗體(JTabbedPane)組件使用案例,需要的朋友可以參考下2014-10-10java數(shù)據(jù)結(jié)構(gòu)實現(xiàn)機(jī)器人行走
這篇文章主要為大家詳細(xì)介紹了java數(shù)據(jù)結(jié)構(gòu)實現(xiàn)機(jī)器人行走,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-01-01Eclipse 2020-06 漢化包安裝步驟詳解(附漢化包+安裝教程)
這篇文章主要介紹了Eclipse 2020-06 漢化包安裝步驟(附漢化包+安裝教程),本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08