JAVA restemplate 通过application/x-www-form-urlencoded访问

发布时间 2023-05-29 14:58:37作者: Asura2017
试了好几种方法都不行,要么返回空,要么报错。
最后就这种方法可以返回数据。


MultiValueMap<String, Object> psp = new LinkedMultiValueMap<>(); psp.add("aaa", "xxxxx"); psp.add("bbb", "xxxxxxxxxxxxx"); psp.add("ccc", "xxxxxxxxx"); psp.add("ddd", "xxxxxxxxxxxxxx"); HttpHeaders headers = new HttpHeaders(); headers.add("Content-Type", "application/x-www-form-urlencoded"); HttpEntity<MultiValueMap<String, Object>> r = new HttpEntity<>(psp, headers); Map responseMessage = getInstance("utf-8").postForObject(PushServiceTypeConfig.MD_PUSH_URL1_PRODUCT, r, Map.class); System.out.println("输出了--------------------------------"); System.out.println(responseMessage);

  
/**
* 创建指定字符集的RestTemplate
*
* @param charset 编码
* @return 返回结果
*/
public static RestTemplate getInstance(String charset) {
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new StringHttpMessageConverter(Charset.forName(charset)));
return restTemplate;
}