我还是Java和Springboot的新手,每次我尝试发送API请求时,我都会卡住。
这是我尝试解析的示例API响应-
{
"respCode": "0",
"statusCode": "0"
}
以下是代码-
MultiValueMap<String, String> body = new LinkedMultiValueMap<>();
body.add("merchantTxnId", enqr.getMerchantTxnId());
body.add("pgTxnId", enqr.getPgTxnId());
RestTemplate restTemplate = RestTemplateClient.getRestTemplate(30, 30);
String signature = "some signature";
HttpHeaders headers = new HttpHeaders();
headers.set("Content-Type","application/json");
headers.set("access_key", accessKey);
headers.set("signature", signature);
headers.set("Accept", "application/json");
HttpEntity<MultiValueMap<String, String>> entity = new
HttpEntity<MultiValueMap<String, String>>(
body, headers);
String url = UriBuilder.fromPath(baseUrl).path("/refund")
.toString();
try {
@SuppressWarnings("rawtypes")
ResponseEntity<Map> ent = restTemplate.exchange(url,
HttpMethod.POST, entity, Map.class);
if (!ent.getBody().get("statusCode").equals("0"))
throw new Exception((String) ent.getBody().get("respMsg"));
} catch (Exception ex) {
logger.info("CouldNot Refund to Source due to {}", ex.getMessage());
return false;
}
在exchange()
通话中出现异常-
org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [interface java.util.Map] and content type [application/octet-stream]