Ribbon RestTemplate GET 请求
程序员文章站
2022-06-25 20:14:04
...
ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://USER-SERVICE/user?name={1}", String.class,"lgt");
String body = responseEntity.getBody();
其中{1}为占位符,若希望返回user对象可以这么写
ResponseEntity<User> responseEntity = restTemplate.getForEntity("http://USER-SERVICE/user?name={1}", String.class,"lgt");
User body = responseEntity.getBody();
若参数为Map类型则需要放进Map再传
Map<String,String> params = new HashMap<>();
params.put("name", "lgt");
ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://USER-SERVICE/user?name={name}", String.class,params);
第二种是getForObject
restTemplate.getForObject(uri, String.class);
restTemplate.getForObject(uri, User.class);
推荐阅读