首页 > 编程语言 > 详细

springboot使用restTemplate post提交值 restTemplate post值

时间:2018-07-21 23:32:57      阅读:199      评论:0      收藏:0      [点我收藏+]
  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);

 

 

springboot使用restTemplate post提交值 restTemplate post值

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!