使用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)文章
Spring框架基于AOP實(shí)現(xiàn)簡(jiǎn)單日志管理步驟解析
這篇文章主要介紹了Spring框架基于AOP實(shí)現(xiàn)簡(jiǎn)單日志管理步驟解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
IntelliJ IDEA Tomcat控制臺(tái)中文亂碼問(wèn)題的四種解決方案
這篇文章主要給大家分享了4種方法完美解決IntelliJ IDEA Tomcat控制臺(tái)中文亂碼問(wèn)題,文中有詳細(xì)的圖文介紹,對(duì)我們的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-08-08
Java中List排序的三種實(shí)現(xiàn)方法實(shí)例
其實(shí)Java針對(duì)數(shù)組和List的排序都有實(shí)現(xiàn),對(duì)數(shù)組而言你可以直接使用Arrays.sort,對(duì)于List和Vector而言,你可以使用Collections.sort方法,下面這篇文章主要給大家介紹了關(guān)于Java中List排序的三種實(shí)現(xiàn)方法,需要的朋友可以參考下2021-12-12
Springboot集成Minio實(shí)現(xiàn)文件上傳基本步驟
這篇文章主要介紹了Springboot集成Minio實(shí)現(xiàn)文件上傳基本步驟,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2025-06-06
Java裝飾器設(shè)計(jì)模式_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了Java裝飾器設(shè)計(jì)模式的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-05-05
java郵件發(fā)送簡(jiǎn)單實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了java郵件發(fā)送簡(jiǎn)單實(shí)現(xiàn)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
一步步教你整合SSM框架(Spring MVC+Spring+MyBatis)詳細(xì)教程
使用SSM(Spring、SpringMVC和Mybatis)已經(jīng)有段時(shí)間了,項(xiàng)目在技術(shù)上已經(jīng)沒(méi)有什么難點(diǎn)了,基于現(xiàn)有的技術(shù)就可以實(shí)現(xiàn)想要的功能,下面這篇文章主要給大家介紹了關(guān)于整合SSM框架:Spring MVC + Spring + MyBatis的相關(guān)資料,需要的朋友可以參考下。2017-07-07

