方法一
将要发送的内容拼接在Url中
HttpEntity<SmsTokenRequestEntity> httpEntity = new HttpEntity<>(requestEntity);
//url 请求地址 + 内容拼接
ResponseEntity<SmsTokenResponseEntity> responseEntity = restTemplate.exchange(url , HttpMethod.POST, httpEntity, typeReference);
方法二
使用
httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
try {
HttpHeaders httpHeaders = new HttpHeaders();
//设置请求内容类型
httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
//使用 MultiValueMap 设置 key-value
MultiValueMap<String, String> body = new LinkedMultiValueMap<>();
body.add("account", requestEntity.getAccount());
body.add("timestamp", requestEntity.getTimestamp());
body.add("nonce",requestEntity.getNonce() );
body.add("sign", requestEntity.getSign());
//添加
HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<>(body, httpHeaders);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<SmsTokenResponseEntity> response = restTemplate.exchange(url, HttpMethod.POST, entity, typeReference);
//转换
SmsTokenResponseEntity bodys = responseEntity.getBody();
} catch (HttpClientErrorException e) {
throw e;
} catch (Exception e) {
e.printStackTrace();
log.info(e.getMessage(), e);
}