一.RestTemplate简介
Spring RestTemplate 是 Spring 提供的用于访问 Rest 服务的客户端,RestTemplate 提供了多种便捷访问远程Http服务的方法,能够大大提高客户端的编写效率,所以很多客户端比如 Android或者第三方服务商都是使用 RestTemplate 请求 restful 服务。
二.实战应用
1.创建实例
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
2.使用
@Autowired
private RestTemplate restTemplate;
(1)get请求-----get请求是通过url传递参数
【1】无参
发送方
String url = "http://localhost:8081/Student/Hello";
ResponseEntity<String> forEntity = restTemplate.getForEntity(url, String.class);
String body = forEntity.getBody();
接收方
@GetMapping("Hello")
public String Hello(){
return "Hello";
}
【2】
发送方
String str ="你好";
String url