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 map= new LinkedMultiValueMap();
map.add("shopid","1");
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
HttpEntity> request = new HttpEntity>(map, headers);
return restTemplate.postForEntity(url, request,String.class);

 

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

 

 

转载于:https://www.cnblogs.com/xinweiyun/p/9348273.html

你可能感兴趣的:(springboot使用restTemplate post提交值 restTemplate post值)