Springboot?HTTP如何調(diào)用其他服務(wù)
更新時(shí)間:2022年01月28日 10:09:44 作者:靜忻寒
這篇文章主要介紹了Springboot?HTTP如何調(diào)用其他服務(wù),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
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)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
解決Springboot get請求是參數(shù)過長的情況
這篇文章主要介紹了解決Springboot get請求是參數(shù)過長的情況,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
Java框架Struts2實(shí)現(xiàn)圖片上傳功能
這篇文章主要為大家詳細(xì)介紹了Java框架Struts2實(shí)現(xiàn)圖片上傳功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08
java?MongoDB實(shí)現(xiàn)列表分頁查詢的示例代碼
本文主要介紹了java?MongoDB實(shí)現(xiàn)列表分頁查詢的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
Spring Boot JPA中使用@Entity和@Table的實(shí)現(xiàn)
這篇文章主要介紹了Spring Boot JPA中使用@Entity和@Table的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
使用Java的Spring框架編寫第一個程序Hellow world
這篇文章主要介紹了Java的Spring框架并用其開始編寫第一個程序Hellow world的方法,Spring是Java的SSH三大web開發(fā)框架之一,需要的朋友可以參考下2015-12-12
SpringBoot如何優(yōu)雅的處理重復(fù)請求
對于一些用戶請求,在某些情況下是可能重復(fù)發(fā)送的,如果是查詢類操作并無大礙,但其中有些是涉及寫入操作的,一旦重復(fù)了,可能會導(dǎo)致很嚴(yán)重的后果,所以本文給大家介紹了SpringBoot優(yōu)雅的處理重復(fù)請求的方法,需要的朋友可以參考下2023-12-12

