RestTemplate介绍
调用远程服务时就必须使用HTTP客户端,主要有四种:JDK原生的URLConnection、Apache的Http Client、Netty的异步HTTP Client, Spring的RestTemplate。
解放了原先HttpClient的复杂提交,java中调用RESTful服务很典型的是使用HttpClient,对于常用的REST操作,这些方法属于低等级的操作。使用HttpClient我们需要自己封装Post请求,再根据响应的状态码判断从响应中获取header和body,有时候还需要自己做json转换。
Spring框架提供的RestTemplate类可用于在应用中调用rest服务,它简化了与http服务的通信方式,统一了RESTful的标准,封装了http链接, 我们只需要传入url及返回值类型即可。相较于之前常用的HttpClient,RestTemplate是一种更优雅的调用RESTful服务的方式。
在Spring应用程序中访问第三方REST服务与使用Spring RestTemplate类有关。RestTemplate类的设计原则与许多其他Spring *模板类(例如JdbcTemplate、JmsTemplate)相同,为执行复杂任务提供了一种具有默认行为的简化方法。
RestTemplate默认依赖JDK提供http连接的能力(HttpURLConnection),如果有需要的话也可以通过setRequestFactory方法替换为例如 Apache HttpComponents、Netty或OkHttp等其它HTTP library。
其实spring并没有真正的去实现底层的http请求(3次握手),而是集成了别的http请求,spring只是在原有的各种http请求进行了规范标准,让开发者更加简单易用,底层默认用的是jdk的http请求。
考虑到RestTemplate类是为调用REST服务而设计的,因此它的主要方法与REST的基础紧密相连就不足为奇了,后者是HTTP协议的方法:HEAD、GET、POST、PUT、DELETE和OPTIONS。例如,RestTemplate类具有headForHeaders()、getForObject()、postForObject()、put()和delete()等方法。
RestTemplate的优缺点:
优点:
连接池、超时时间设置、支持异步、请求和响应的编解码
缺点:
依赖别的spring版块、参数传递不灵活
RestTemplate官方网站:https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/client/RestTemplate.html
一、依赖包
RestTemplate是spring的一个rest客户端,在spring-web这个包下,spring boot的依赖如下:
org.springframework.boot
spring-boot-starter-web
点击spring-boot-starter-web依赖会打开spring-web
org.springframework
spring-web
5.2.2.RELEASE
compile
Get请求接口
/**
* 带参数,返回list集合的get请求
* @return
*/
@GetMapping("/getMethodHasParamReturnList")
public ResponseEntity getMethodHasParamReturnList(){
Map param=new HashMap<>();
param.put("pageNum",1);
param.put("pageSize",2);
HttpHeaders headers=new HttpHeaders();
ResponseEntity response = restTemplate.exchange(
"http://127.0.0.1:8080/getPost/getMethodHasParamReturnList?pageNum={pageNum}&pageSize={pageSize}",
HttpMethod.GET,
new HttpEntity(headers),
List.class,
param);
System.out.println(response.getBody());
return response;
}
远程调用接口:
/**
* 带参数,返回list集合的get请求
* @return
*/
@GetMapping("/getMethodHasParamReturnList")
public List getMethodHasParamReturnList(
@RequestParam(value = "pageNum",required = false) Integer pageNum,
@RequestParam(value = "pageSize",required = false) Integer pageSize){
Student student1=new Student();
student1.setId("2222");
student1.setName("lisi");
Student student2=new Student();
student2.setId("3333");
student2.setName("wangwu");
List students=new ArrayList<>();
students.add(student1);
students.add(student2);
return students;
}
Post请求接口
/**
* post请求,带参数,返回list集合
* @param
* @return
*/
@PostMapping("/postMethodReturnList")
public List postMethodReturnList(){
//请求头
MediaType type=MediaType.parseMediaType("application/json");
HttpHeaders headers = new HttpHeaders();
headers.setContentType(type);
//请求参数
Student student=new Student();
student.setId("1111");
student.setName("aaaa");
//这里要把数据转为json格式才能传递
Object param = JSON.toJSON(student);
HttpEntity formEntity = new HttpEntity<>(param.toString(), headers);
ResponseEntity responsebody = restTemplate.exchange(
"http://127.0.0.1:8080/getPost/postMethodReturnList",
HttpMethod.POST,
formEntity,
List.class);
List body = responsebody.getBody();
System.out.println(body);
return body;
}
远程请求接口:
/**
* post请求,带参数,返回list集合
* @param student
* @return
*/
@PostMapping("/postMethodReturnList")
public List postMethodReturnList(@RequestBody Student student){
List students=new ArrayList<>();
students.add(student);
students.add(student);
students.add(student);
System.out.println(students);
return students;
}
参考文章:
https://www.cnblogs.com/h--d/p/12609753.html
https://blog.csdn.net/QiaoRui_/article/details/80453799
配置连接池比较好的文章
https://blog.csdn.net/umke888/article/details/54881946
https://www.cnblogs.com/likaitai/p/5431246.html
讲解比较好的文章
https://my.oschina.net/lifany/blog/688889