RestTemplate中几种常见的请求方式
GET请求 第一种:getForEntity getForEntity方法的返回值是一个 ResponseEntity<T> , ResponseEntity<T> 是Spring对HTTP请求响应的封装,包括了几个重要的元素,如响应码、contentType、contentLength、响应消息体等。 可以用一个数字做占位符,最后是一个可变长度的参数,来一一替换前面的占位符 @RequestMapping("/sayhello") public String sayHello() { ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://HELLO-SERVICE/sayhello?name={1}", String.class, "张三"); return responseEntity.getBody(); } 可以前面使用name={name}这种形式,最后一个参数是一个map,map的key即为前边占位符的名字,map的value为参数值 @RequestMapping("/sayhello2") public String sayHello2() { Map<String, String> map = new HashMap<>(); map.put("name", "李四");