最近使用Spring 的 RestTemplate 工具类请求接口的时候发现参数传递的一个坑,也就是当我们把参数封装在Map里面的时候,Map 的类型选择。 使用RestTemplate post请求的时候主要可以通过三种方式实现
1、调用postForObject方法 2、使用postForEntity方法 3、调用exchange方法
postForObject和postForEntity方法的区别主要在于可以在postForEntity方法中设置header的属性,当需要指定header的属性值的时候,使用postForEntity方法。exchange方法和postForEntity类似,但是更灵活,exchange还可以调用get、put、delete请求。使用这三种方法调用post请求传递参数,Map不能定义为以下两种类型(
url使用占位符进行参数传递时除外)
Map<String, Object> paramMap = new HashMap<String, Object>(); Map<String, Object> paramMap = new LinkedHashMap<String, Object>();
经过测试,我发现这两种map里面的参数都不能被后台接收到,这个问题困扰我两天,终于,当我把Map类型换成LinkedMultiValueMap后,参数成功传递到后台。
MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<String, Object>();
注:HashMap是以请求体传递,MultiValueMap是表单传递。
经过测试,正确的POST传参方式如下
public static void main(String[] args) { RestTemplate template = new RestTemplate(); String url = "http://192.168.2.40:8081/channel/channelHourData/getHourNewUserData"; // 封装参数,千万不要替换为Map与HashMap,否则参数无法传递 MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<String, Object>(); paramMap.add("dt", "20180416"); // 1、使用postForObject请求接口 String result = template.postForObject(url, paramMap, String.class); System.out.println("result1==================" + result); // 2、使用postForEntity请求接口 HttpHeaders headers = new HttpHeaders(); HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(paramMap,headers); ResponseEntity<String> response2 = template.postForEntity(url, httpEntity, String.class); System.out.println("result2====================" + response2.getBody()); // 3、使用exchange请求接口 ResponseEntity<String> response3 = template.exchange(url, HttpMethod.POST, httpEntity, String.class); System.out.println("result3====================" + response3.getBody()); }
补充:POST传参对象
@Autowired private RestTemplate restTemplate; private String url="http://localhost:8080/users"; public Integer save(User user){ Map<String,String> map = restTemplate.postForObject(url, user, Map.class); if(map.get("result").equals("success")){ //添加成功 return 1; } return -1; } //这是访问的controller方法 @RequestMapping(value = "users",method = RequestMethod.POST) public Map<String,String> save(@RequestBody User user){ Integer save = userService.save(user); Map<String,String> map=new HashMap<>(); if(save>0){ map.put("result","success"); return map; } map.put("result","error"); return map; }
ps:post请求也可以通过占位符的方式进行传参(类似get),但是看起来不优雅,推荐使用文中的方式。
GET方式传参说明
如果是get请求,又想要把参数封装到map里面进行传递的话,Map需要使用HashMap,且url需要使用占位符,如下:
public static void main(String[] args) { RestTemplate restTemplate2 = new RestTemplate(); String url = "http://127.0.0.1:8081/interact/getData?dt={dt}&ht={ht}"; // 封装参数,这里是HashMap Map<String, Object> paramMap = new HashMap<String, Object>(); paramMap.put("dt", "20181116"); paramMap.put("ht", "10"); //1、使用getForObject请求接口 String result1 = template.getForObject(url, String.class, paramMap); System.out.println("result1====================" + result1); //2、使用exchange请求接口 HttpHeaders headers = new HttpHeaders(); headers.set("id", "lidy"); HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(null,headers); ResponseEntity<String> response2 = template.exchange(url, HttpMethod.GET, httpEntity, String.class,paramMap); System.out.println("result2====================" + response2.getBody()); }
RestTemplate提供的delete()和put()方法都没有返回值,但是我要调用的接口是有返回值的,网上的资料很多都是写的调用exchange()方法来实现,但是基本上都没有给出完整实例,导致我在参考他们的代码的时候会出现参数无法传递的问题,目前我已经解决该问题,现将我的解决方法分享出来
我同样是使用exchange()方法来实现,但是url有讲究,需要像使用exchange方法调用get请求一样,使用占位符
delete请求实例,请求方式使用 HttpMethod.DELETE(resultful风格使用占位符)
/** * 删除用户 * @param id * @return */ public String delete(Long id) { StringBuffer url = new StringBuffer(baseUrl) .append("/user/delete/{id}"); Map<String, Object> paramMap = new HashMap<>(); paramMap.put("id", id); ResponseEntity<String > response = restTemplate.exchange(url.toString(), HttpMethod.DELETE, null, String .class, paramMap); String result = response.getBody(); return result; }
补充:resultful风格直接拼接url
//负责调用provider的方法,获取数据 @Autowired private RestTemplate restTemplate; //在provider端资源的路径 private String url="http://localhost:8080/details"; public String deleteDetail(Integer id){ ResponseEntity<String> response = restTemplate.exchange(url + "/" + id, HttpMethod.DELETE, null, String.class); String result = response.getBody(); return result; } //被调用的controller方法 @ResponseBody @RequestMapping(value = "details/{id}",method = RequestMethod.DELETE) public String deleteDetail(@PathVariable Integer id){ Integer integer = detailService.deleteDetail(id); if(integer>0){ return "success"; } return "error"; }
不是resultful风格可以使用占位符
private String url="http://localhost:8080/details?id={id}"; public String deleteDetail(Integer id){ Map<String, Object> paramMap = new HashMap<>(); paramMap.put("id", id); ResponseEntity<String > response = restTemplate.exchange(url.toString(), HttpMethod.DELETE, null, String .class, paramMap); String result = response.getBody(); return result; }
put请求实例,请求方式使用 HttpMethod.PUT
/** * 更新用户基础信息 * @param userInfoDTO * @return */ public String edit(UserInfoDTO userInfoDTO) { StringBuffer url = new StringBuffer(baseUrl) .append("/user/edit?tmp=1") .append("&id={id}") .append("&userName={userName}") .append("&nickName={nickName}") .append("&realName={realName}") .append("&sex={sex}") .append("&birthday={birthday}"); Map<String, Object> paramMap = new HashMap<>(); paramMap.put("userId", userInfoDTO.getId()); paramMap.put("userName", userInfoDTO.getUserName()); paramMap.put("nickName", userInfoDTO.getNickName()); paramMap.put("realName", userInfoDTO.getRealName()); paramMap.put("sex", userInfoDTO.getSex()); paramMap.put("birthday", userInfoDTO.getBirthday()); ResponseEntity<String > response = restTemplate.exchange(url.toString(), HttpMethod.PUT, null, String .class, paramMap); String result = response.getBody(); return result; }
再次补充exchange()传参对象:
//测试post的controller @RequestMapping(value = "detailsPost",method = RequestMethod.POST) public String test02(@RequestBody Detail detail){ System.out.println("POST-"+detail); return "error"; } //测试put的controller @RequestMapping(value = "detailsPut",method = RequestMethod.PUT) public String test03(@RequestBody Detail detail){ System.out.println("PUT-"+detail); return "error"; } //测试delete的controller @RequestMapping(value = "detailsDelete",method = RequestMethod.DELETE) public String test04(@RequestBody Detail detail){ System.out.println("DELETE-"+detail); return "error"; } //测试方法 public String test(){ //put传递对象 //String json = "{\"author\":\"zsw\",\"createdate\":1582010438846,\"id\":1,\"summary\":\"牡丹\",\"title\":\"菏泽\"}"; //HttpHeaders headers = new HttpHeaders(); //headers.setContentType(MediaType.APPLICATION_JSON); //HttpEntity<String> entity = new HttpEntity<>(json,headers); //ResponseEntity<String> resp = restTemplate.exchange("http://localhost:8080/detailsPut", HttpMethod.PUT, entity, String.class); //delete传递对象 Detail detail=new Detail(); detail.setId(1L); detail.setSummary("牡丹"); detail.setTitle("菏泽"); detail.setAuthor("zsw"); detail.setCreatedate(new Timestamp(new Date().getTime())); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); HttpEntity<Detail> entity = new HttpEntity<>(detail,headers); ResponseEntity<String> resp = restTemplate.exchange("http://localhost:8080/detailsDelete", HttpMethod.DELETE, entity, String.class); String result = resp.getBody(); System.out.println(result); return result; }
delete请求和上面一样,但是get不行,直接报错400。好像是get不支持这种传参。https://blog.belonk.com/c/http_resttemplate_get_with_body.htm 和这大哥的情况一样,但是他的解决方案我没搞明白,so 如有大佬还望指点一下老弟,不胜感激。
exchange()传递单个参数可以使用占位符:
//post传递单参 // ResponseEntity<String> resp = restTemplate.exchange("http://localhost:8080/detailsPostD?id={id}&name={name}", HttpMethod.POST, null, String.class,1,"zsw"); //put传递单参 Map<String,Object> map=new HashMap<>(); map.put("id",1); map.put("name","zsw"); ResponseEntity<String> resp = restTemplate.exchange("http://localhost:8080/detailsPutD?id={id}&name={name}", HttpMethod.PUT, null, String.class,map);
get、post、put、delete请求通用。