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

