使用 RestTemplate 发送 post 请求传递参数

注意:

post 请求的参数不能用 HashMap 封装,应该使用 MultiValueMap

写法参考

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

MultiValueMap<String, String> map= new LinkedMultiValueMap<String, String>();
map.add("email", "[email protected]");

HttpEntityString, String>> request = new HttpEntityString, String>>(map, headers);

ResponseEntity<String> response = restTemplate.postForEntity( url, request , String.class );

参考资料:

1、How to POST form data with Spring RestTemplate?

https://stackoverflow.com/questions/38372422/how-to-post-form-data-with-spring-resttemplate

2、Using Spring RestTemplate to POST params with objects

https://stackoverflow.com/questions/25050617/using-spring-resttemplate-to-post-params-with-objects

3、RestTemplate post如何传递参数
http://www.cnblogs.com/shoren/p/RestTemplate-problem.html


你可能感兴趣的:(JavaWeb基础)