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

