RestTemplate 发送post请求

springboot使用restTemplate post提交值 restTemplate post值
post提交有 FormData和Payload 两种形式:
第一种是formdata形式,在header参数里可以直接看到
payload则封装成json格式post过去,获取以后需要再解析成实体。

restTemplate post json格式
使用阿里巴巴的json包 com.alibaba.fastjson
代码demo如下:

url='http://posturl';JSONObject postData = new JSONObject();
postData.put("shopid", 1);
JSONObject json = restTemplate.postForEntity(url, postData, JSONObject.class).getBody();

如果要使用post formdata形式则需要
使用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);

posted @ 2019-08-08 10:42  雷神约  阅读(48597)  评论(0编辑  收藏  举报