一:什么是RestTemplate?
RestTemplate是Spring3.0框架提供的一个HTTP请求工具,它提供了常见的rest请求的方案,例如。GET,POST,PUT,DELETE请求,等。使用RestTemplate可以让我们更加简洁,高效的进行HTTP请求服务的调用。
二:如何使用RestTemplate进行发送http请求。
RestTemplate是spring框架提供的,我们这里使用一个springBoot项目举例子。大家不知道怎么配置的可以自行网上查询。
SpringBoot项目不需要进行额外的导包。可以直接使用RestTemplate。需要注意的重点是,RestTemplate是没有交由SpringBean容器管理的,为了方便使用我们可以,编写一个配置类,将RestTemplate交给Spring的容器进行管理。
//标识这是一个配置类
@Configuration
public class BeanUtils {
@Bean("restTemplate")
public RestTemplate RestTemplate(){
return new RestTemplate();
}
}
标识之后,RestTemplate类就可以使用属性注入的方式,进行使用了
@Service
public class RestTemplateService {
@Autowired
private RestTemplate restTemplate;
}
三:RestTemplate包含方法
我们把这些方法,归纳一下,提炼出其中属于http请求的方法。
GET
POST
PUT
DELETE
HEAD
OPTIONS
EXCHANGE
EXECUTE
四:具体使用
4.1 使用RestTemplate发送get请求。
get请求一共分为两类,getForObject和getForEntity,其中每一类有三个重载方法。
gerForEntity
首先在Controller里,提供一个Get类型的方法
@RestController
public class UserController {
@GetMapping("/hello")
public String sayHello(String name){
return "hello"+name;
}
}
通过getForEntity,我们不仅可以获得响应数据,还可以获得响应头。get请求可以通过3中方式传递参数
/**
* @description:占位符拼接参数
* @author: haolaosan
* @date: 2023/2/28
* @param: []
* @return: java.lang.String
**/
public String getForEntity(){
String url = "http://localhost:8090/restTemplate" +"/hello?name={1}";
ResponseEntity<String> response = restTemplate.getForEntity(url, String.class, "haolaosan");
HttpHeaders headers = response.getHeaders();
System.out.println("响应头headers===="+headers);
String responseBody = response.getBody();
System.out.println("响应体body===="+responseBody);
HttpStatus statusCode = response.getStatusCode();
System.out.println("响应状态码====" + statusCode);
return responseBody;
}
/**
* @description:map传递参数
* @author: haolaosan
* @date: 2023/2/28
* @param: []
* @return: java.lang.String
**/
public String getForEntity2(){
String url = "http://localhost:8090/restTemplate" +"/hello?name={name}";
Map<String,String> map = new HashMap<>();
map.put("name","maphaolaosan");
ResponseEntity<String> forEntity = restTemplate.getForEntity(url, String.class, map);
return forEntity.getBody();
}
/**
* @description:参数直接写到url地址内
* @author: haolaosan
* @date: 2023/2/28
* @param: []
* @return: java.lang.String
**/
public String getForEntity3(){
String url = "http://localhost:8090/restTemplate/hello?name=郝立琢直接拼参";
ResponseEntity<String> forEntity = restTemplate.getForEntity(url, String.class);
return forEntity.getBody();
}
通过观察上文我们可以发现,RestTemplate的getForEntity方法返回的是一个ResponseEntity类型的对象。我们可以通过,getHeads,getBody,getStatusCode方法,分别获取,响应头,响应体,响应状态码。
getForObject
getForObject与getForEntity没有太大不同,他们的不同主要在于,getForObject返回的直接是body体,如果不需要获取响应头,响应状态码等信息,那直接使用getForObject方法无疑会更加方便。
/**
* @description:
* @author: 占位符拼接参数,直接获取响应体
* @date: 2023/2/28
* @param: []
* @return: java.lang.String
**/
public String getForObject(){
String url = "http://localhost:8090/restTemplate" +"/hello?name={1}";
String forObject = restTemplate.getForObject(url, String.class, "郝立琢obj");
return forObject;
}
/**
* @description:map传递参数
* @author: haolizhuo
* @date: 2023/2/28
* @param: []
* @return: java.lang.String
**/
public String getForObject2(){
String url = "http://localhost:8090/restTemplate" +"/hello?name={name}";
Map<String,String> map = new HashMap<>();
map.put("name","map郝立琢");
String forObject = restTemplate.getForObject(url, String.class, map);
return forObject;
}
/**
* @description:参数直接写到url地址内
* @author: haolizhuo
* @date: 2023/2/28
* @param: []
* @return: java.lang.String
**/
public String getForObject3(){
String url = "http://localhost:8090/restTemplate/hello?name=郝立琢直接拼参";
String forObject = restTemplate.getForObject(url, String.class);
return forObject;
}
4.2 使用RestTemplate发送post请求
post请求和get请求一样,也有两种发送方式,第一种是postForEntity,另一种是postForObject。和get请求一样,也是是否获取响应头
@Autowired
private RestTemplate restTemplate;
/**
* @description:post请求,使用forEntity
* @author: haolizhuo
* @date: 2023/3/1
* @param: []
* @return: java.lang.String
**/
public String postForEntity(){
String url ="http://localhost:8090/restTemplate/hello2";
//使用key,value的方式进行调用
Map<String,Object> map = new HashMap<>(16);
map.put("name","郝立琢post,map传参");
ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, map, String.class);
HttpHeaders headers = responseEntity.getHeaders();
System.out.println("响应头headers===="+headers);
String responseBody = responseEntity.getBody();
System.out.println("响应体body===="+responseBody);
HttpStatus statusCode = responseEntity.getStatusCode();
System.out.println("响应状态码====" + statusCode);
return responseEntity.getBody();
}
/**
* @description:post请求,使用forEntity,传递json数据
* @author: haolizhuo
* @date: 2023/3/1
* @param: []
* @return: java.lang.String
**/
public String postForEntity2(){
String url ="http://localhost:8090/restTemplate/hello2";
//使用JSON数据进行传输,在post请求中,可以自动将一个对象转换为JSON数据进行传输
UserModel userModel = new UserModel();
userModel.setName("郝立琢使用JSON数据传输");
ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, userModel, String.class);
return responseEntity.getBody();
}
/**
* @description:post请求,使用ForObject,传递json数据
* @author: haolizhuo
* @date: 2023/3/1
* @param: []
* @return: java.lang.String
**/
public String postForObject(){
String url ="http://localhost:8090/restTemplate/hello2";
//使用JSON数据进行传输,在post请求中,可以自动将一个对象转换为JSON数据进行传输
UserModel userModel = new UserModel();
userModel.setName("郝立琢调用postForObject");
String postForObject = restTemplate.postForObject(url, userModel, String.class);
return postForObject;
}
与get请求不同的是,post请求的传参,是通过json形式传递的,无论我们是使用自己定义的实体进行传参,还是使用map集合进行传参,最后都会被解析成JSON。具体如何选择,应该与我们的业务结合。如果是调用三方,并且不常用,那么我们可以选择直接使用map进行传参就够了,可如果要经常使用这些数据,还是老老实实搞一个model类吧!
总结
本文只列出了,get请求与post的请求的使用,至于其他的,put,delete请求,与get和post请求的方式都相差不多,我将这个RestTemplate的例子传到了网盘上,大家如果有兴趣可以下载研究。
链接: https://pan.baidu.com/s/1yidh07u5I6MF_yLkls1ycA?pwd=gkg5 提取码: gkg5 复制这段内容后打开百度网盘手机App,操作更方便哦