RestTemplate可以用来调用其他服务,包括get、post、delete、put。这里,我将举例展示get和post的方法。
1.get请求
我们现在提供服务的项目中新建一个People类,作为返回数据的对象
public class People {
private String name;
private String sex;
private String age;
private String job;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
}
修改我们的controller,将收到得对象返回
@RestController
public class HelloControler {
private final Logger logger = Logger.getLogger(getClass());
@Autowired
private DiscoveryClient client;
@RequestMapping("/hello")
public People sayHello(People people){
ServiceInstance instance = client.getLocalServiceInstance();
logger.info("/hello,host"+instance.getHost()+",server_id:"+instance.getServiceId());
if (people.getName() == null){
people.setSex("男");
people.setName("无参数");
people.setJob("无业");
people.setAge("12");
}
return people;
}
}
将People类复制到我们要调用该服务的项目中,以便在用RestTemplate调用时作为参数格式,然后编写调用hello服务的接口
@Autowired
private RestTemplate restTemplate;
@RequestMapping("/templateGet")
public Map templateGet() {
Map result = new HashMap();
ResponseEntity<People> forEntity = restTemplate.getForEntity("http://hello-service/hello", People.class);
People body = forEntity.getBody();
result.put("无参数的结果",body);
String arr[] = {"数组","18","男","程序员"};
ResponseEntity<People> arrEntity = restTemplate.getForEntity("http://hello-service/hello?name={1}&age={2}&sex={3}&job={4}", People.class, arr);
People arrPeople = arrEntity.getBody();
result.put("数组作为参数",arrPeople);
Map param = new HashMap();
param.put("name","map");
param.put("age","19");
param.put("sex","男");
param.put("job","攻城狮");
ResponseEntity mapEntity = restTemplate.getForEntity("http://hello-service/hello?name={name}&age={age}&sex={sex}&job={job}", People.class, param);
Object mapPeople = mapEntity.getBody();
result.put("map作为参数",mapPeople);
return result;
}
启动项目调用后,结果如下:
可以看到restTemplate.getForEntity方法有三个重载的方法。
第一个可以将参数直接拼装在url后面,第二个参数就是要接收的body的类型定义。
第二个是以数组作为参数,其中参数与值得对应是按照数组的顺序对应起来,会替换掉网址的占位符1,2,3
第三个是以map作为参数,参数与值的对应关系是按照map的key取值对应。
getForEntity方法返回的对象是RresponseEntity,通过它的getBody可以获取到服务返回我们的数据。
RestTemplate发送get请求还可以通过getForObject函数来获取,直接返回我们需要的数据类型格式,不需要再通过RresponseEntity的getBody方法获取,同样有三种不同的重载实现,在不需要关注除了body以外的数据时很方便,用法基本类似,这里不再专门举例;
2.post请求
@RequestMapping("/templatePost")
public People templatePost() {
MultiValueMap<String,String> headers = new LinkedMultiValueMap();
MultiValueMap param = new LinkedMultiValueMap();
param.add("name","map");
param.add("age","19");
param.add("sex","男");
param.add("job","攻城狮");
HttpEntity entity = new HttpEntity(param,headers);
ResponseEntity<People> peopleResponseEntity = restTemplate.postForEntity("http://hello-service/hello", entity, People.class);
People body = peopleResponseEntity.getBody();
return body;
}
这里HttpEntity可以设置参数和header等参数,其他使用方法与get请求基本相同