springboot使用restTemplate post提交值 restTemplate post值

匿名 (未验证) 提交于 2019-12-03 00:43:02
第一种是formdata形式,在header参数里可以直接看到
代码demo如下:
url=‘http://posturl‘;JSONObject postData = new JSONObject();      
postData.put("shopid", 1);
JSONObject json = restTemplate.postForEntity(url, postData, JSONObject.class).getBody();

使用RestTemplate发送multipart/form-data格式的数据
String url = ‘http://posturl‘;
MultiValueMap<String, String> map= new LinkedMultiValueMap<String, String>();
map.add("shopid","1");
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(map, headers);
return restTemplate.postForEntity(url, request,String.class);

对header进行请求头设置,如果不设置也可以直接post那么就是如下的
String url = ‘http://posturl‘;         MultiValueMap<String, String> map= new LinkedMultiValueMap<String, String>();          map.add("shopid","1");
return restTemplate.postForEntity(url, map,String.class);

原文:https://www.cnblogs.com/xinweiyun/p/9348273.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!