RestTemplate之form表单提交
<ul>
<li>form表单工具类:</li>
</ul>
<pre><code>public static JSONObject postByForm(HashMap<String, String> hashMap, String url) {
logger.info("postByForm: url:{} req:{}", url, hashMap.toString());
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, String> params = new LinkedMultiValueMap();
Set<Map.Entry<String, String>> map = hashMap.entrySet();
for (Map.Entry<String, String> stringStringEntry : map) {
params.add(stringStringEntry.getKey(), stringStringEntry.getValue());
}
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity(params, headers);
ResponseEntity<JSONObject> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, JSONObject.class);
JSONObject body = response.getBody();
logger.info("postByForm: url:{} resq:{}", url, body);
return body;
}</code></pre>