使用restTemplate.postForEntity()的問題
使用restTemplate.postForEntity()
@Component public class RemoteQuestUtil { @Autowired private RestTemplate restTemplate; public String send(String srvcCode, String request){ //srvcCode 獲取對應交易 現場適配 MockPropertiesUtil instance = MockPropertiesUtil.getInstance(); String url = instance.getProperty(srvcCode); //處理接收接口只支持JSONOBject HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); HttpEntity request1 = new HttpEntity<>(request, headers); log.info("請求報文:{}",request); ResponseEntity<String> jsonObjectResponseEntity = restTemplate.postForEntity(url, request1, String.class); String resp = jsonObjectResponseEntity.getBody(); log.info("響應報文:{}",resp); return resp; } }
MockPropertiesUtil
public class MockPropertiesUtil { public static Properties pro = new Properties(); public static MockPropertiesUtil instance = new MockPropertiesUtil(); public static MockPropertiesUtil getInstance() { return instance; } static { InputStream in ; try { in = MockPropertiesUtil.class.getClassLoader().getResourceAsStream("mock-properties.properties"); //加載文件將文件加載為 pro = new Properties(); pro.load(in); } catch (IOException e) { log.error("加載Mock配置文件[mock-properties.properties]時異常",e); throw new PlatformException(PlatformError.LOADCONFIG_ERROR); } try { if (in != null){ in.close(); } } catch (IOException e) { log.error("關閉流異常",e); } } public String getProperty(String key) { if (StringUtil.isEmpty(key)) { log.warn("key is null."); return null; } if (!pro.containsKey(key)){ log.info("無此服務碼:{}",key); throw new PlatformException(PlatformError.GET_URL_BY_CODE_ERROR); } //通過鍵值獲得對應的url key=url String url = pro.getProperty(key); log.info("服務碼:{},對應的url為:{}",key,url); return url; } }
RestTemplate().postForEntity的參數
RestTemplate().postForEntity() 是 Spring Framework 提供的一個用于發(fā)送 HTTP POST 請求并獲取響應的方法。
以下是該方法的參數詳解
url
(String 類型):請求的目標 URL??梢允且粋€字符串形式的 URL,也可以是一個 URI 對象。示例:“http://example.com/api”。request
(Object 類型):表示要發(fā)送的請求體內容??梢允且粋€簡單對象、一個 HttpEntity 對象或一個 MultiValueMap(用于傳遞表單數據)。根據實際需要確定所需的請求體內容。responseType
(Class 類型):表示期望的響應類型。可以是任何 Java 類型,包括自定義類型。例如,如果期望返回一個 User 對象,則可以將其設置為 User.class。uriVariables
(Object… 類型):可選參數,用于填充 URL 中的占位符。如果 URL 中包含占位符,可以通過這個參數來提供具體的值。uri
(URI 類型):可選參數,代替 url 參數,用于指定完整的請求目標 URI。
注意事項
- 如果使用 url 參數,uriVariables 參數將用于替換 URL 中的占位符。
- 如果使用 uri 參數,則忽略 url 和 uriVariables 參數。
- 如果請求需要設置請求頭或其他配置信息,可以使用 HttpEntity 對象構建請求。
方法返回一個 ResponseEntity 對象,其中包含 HTTP 響應的狀態(tài)碼、響應頭以及解析后的響應體。你可以通過 ResponseEntity 對象獲取所需的數據。
以下是一個使用 RestTemplate().postForEntity() 方法發(fā)送 POST 請求的示例代碼:
import org.springframework.http.*; import org.springframework.web.client.RestTemplate; public class RestTemplateExample { public static void main(String[] args) { RestTemplate restTemplate = new RestTemplate(); // 請求 URL String url = "http://example.com/api"; // 構建請求體 User user = new User("John", 30); // 自定義 User 類 HttpEntity<User> request = new HttpEntity<>(user); // 發(fā)送 POST 請求并獲取響應 ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class); // 獲取響應結果 HttpStatus statusCode = response.getStatusCode(); HttpHeaders headers = response.getHeaders(); String body = response.getBody(); // 處理響應結果 System.out.println("Status Code: " + statusCode); System.out.println("Response Headers: " + headers); System.out.println("Response Body: " + body); } }
也可以使用Map傳遞Json數據,
例如:
Map<String, Object> requestMap = new HashMap<>(); // 發(fā)動機型號 requestMap.put("engine_model", "1234"); // 發(fā)動機編號 requestMap.put("engine_code", "6789"); // 請求 URL String url = "http://example.com/api"; // 調對方接口 ResponseEntity<String> responseEntity = new RestTemplate().postForEntity(url, requestMap , String.class); Map<String, Object> responseBodyMap = GsonUtil.gsonToMaps(responseEntity.getBody()); // 對方接口返回值 true傳輸成功 false 失敗 Map<String, Object> result = (Map<String, Object>) responseBodyMap.get("result"); boolean isSuccess = (Boolean) result.get("success");
根據上述的布爾值判斷接口是否調用成功,進行后續(xù)邏輯。
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
解決springboot jpa @Column columnDefinition等屬性失效問題
這篇文章主要介紹了解決springboot jpa @Column columnDefinition等屬性失效問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10通過AOP攔截Spring?Boot日志并將其存入數據庫功能實現
本文介紹了如何使用Spring Boot和AOP技術實現攔截系統(tǒng)日志并保存到數據庫中的功能,包括配置數據庫連接、定義日志實體類、定義日志攔截器、使用AOP攔截日志并保存到數據庫中等步驟,感興趣的朋友一起看看吧2023-08-08如何解決@SpringBootApplication報錯問題
這篇文章主要介紹了如何解決@SpringBootApplication報錯問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07IDEA中l(wèi)og4j 無法輸出到本地 properties配置無效問題
這篇文章主要介紹了IDEA中l(wèi)og4j 無法輸出到本地 properties配置無效問題,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-10-10SpringCloud?Feign實現微服務之間相互請求問題
Feign是Netflix開發(fā)的聲明式、模板化的HTTP客戶端,?Feign可以幫助我們更快捷、優(yōu)雅地實現微服務之間的調用,這篇文章主要介紹了SpringCloud?Feign實現微服務之間相互請求,需要的朋友可以參考下2022-06-06SpringBoot集成FastDFS依賴實現文件上傳的示例
這篇文章主要介紹了SpringBoot集成FastDFS依賴實現文件上傳,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-05-05IDEA 集成 Docker 插件一鍵部署 SpringBoot 應用
通過本文介紹的方法,我們期望能幫助開發(fā)者更輕松地在IDEA中實現Spring Boot應用的Docker化部署,為現代軟件開發(fā)提供更便捷的解決方案,感興趣的朋友一起看看吧2023-11-11使用Okhttp實現上傳文件+參數請求接口form-data
在進行接口對接時,常遇到需要傳遞多種類型參數及文件上傳的情況,解決此問題的關鍵在于參數傳遞和文件上傳的正確處理,在Service層和Controller層的傳參,可以通過@RequestParam標注或直接使用請求實體類,但若結合文件上傳,則不應使用@RequestBody注解2024-10-10