spring RestTemplate 发送post 请求

MultiValueMap<String, String> dataParam = new LinkedMultiValueMap<String, String>();  // 需要传递的参数

例如:dataParam .put("name","zhangsan");

HttpHeaders headers = new HttpHeaders();  //设置请求头

headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<MultiValueMap<String, String>>(dataParam ,
headers);
RestTemplate restTemplate = new RestTemplate();
String str = restTemplate.postForObject(url, entity, String.class);//url :请求的地址;entity : http请求实体 ;返回值类型

posted @ 2019-01-16 16:51  zhangjunlong  阅读(248)  评论(0编辑  收藏  举报