RestTemplate 的使用
- 什么是RestTemplate
RestTemplate是Spring 框架提供的基于REST的服务组件,底层是对HTTP请求及响应进行了封装,提供了很多访问REST服务的方法,可以简化代码开发 - 如何使用RestTemplate
1、创建Maven工程,pom.xml (这里我直接建在前面建好的父工程里面,只需要springboot依赖即可,就已经包含了RestTemplate 方法)
取名为:resttemplate
2、把上一步中创建好的实体类复制过来
3、在java文件夹中新建启动类,并且把RestTemplate 实例注入到ioc里,并且新建controller,取名为RestHandler
RestTemplateApplication代码如下:
package com.southwind;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
public class RestTemplateApplication {
public static void main(String[] args){
SpringApplication.run(RestTemplateApplication.class);
}
//在这里注入RestTemplate实例,这样就可以使用RestTemplate
@Bean //把RestTemplate 添加到ioc容器
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
RestHandler 代码内容如下:
这里使用RestTemplate方法实现模块间的调用
总共有两个方法:
- .getForEntity() 方法返回的类型是通过 ResponseEntity包裹的泛型,所以如果想要自定义返回的类型就需要调用.getBody()方法,不然就只能把返回的类型包裹进ResponseEntity<>里面
- .getForObject() 方法 返回的就是泛型,所以不需要在调用其他方法,直接是定义的方法里面返回的是什么类型就返回什么类型
package com.southwind.controller;
import com.southwind.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import java.util.Collection;
@RestController
@RequestMapping("/rest")
public class RestHandler {
@Autowired
private RestTemplate restTemplate;
//在这里通过RestTemplate的方法调用 eruekaclient 模块的方法
//第一种方法,使用getForEntity,返回的是 ResponseEntity<T>,所以需要通过调用.getBody()方法
@GetMapping("/findAll")
public Collection<Student> findAll(){
return restTemplate.getForEntity("http://localhost:8010/student/findAll",Collection.class).getBody();
}
//第二种方法,使用getForObject ,因为此方法返回值就是泛型 ,所以直接返回即可
@GetMapping("/findAll2")
public Collection<Student> findAll2(){
return restTemplate.getForObject("http://localhost:8010/student/findAll",Collection.class);
}
@GetMapping("/findById/{id}")
public Student findById(@PathVariable("id") long id){
return restTemplate.getForEntity("http://localhost:8010/student//findById/{id}",Student.class,id).getBody();
}
@GetMapping("/findById2/{id}")
public Student findById2(@PathVariable("id") long id){
return restTemplate.getForObject("http://localhost:8010/student/findById/{id}",Student.class,id);
}
@PostMapping("/save")
public void save(@RequestBody Student student){
restTemplate.postForEntity("http://localhost:8010/student/save",student,null).getBody();
}
@PostMapping("/save2")
public void save2(@RequestBody Student student){
restTemplate.postForObject("http://localhost:8010/student/save",student,void.class);
}
@PutMapping("/update")
public void update(@RequestBody Student student){
restTemplate.put("http://localhost:8010/student/update",student);
}
@DeleteMapping("deleteById/{id}")
public void deleteById(@PathVariable("id") long id){
restTemplate.delete("http://localhost:8010/student/deleteById/{id}",id);
}
}
Student 代码如下:
package com.southwind.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor //无参构造
@AllArgsConstructor //有参构造
public class Student {
private long id;
private String name;
private int age;
}
完成,启动eurekaclient 工程(第二部创建的服务提供者的工程),启动resttemplate 工程,使用postman 测试增删改查
这里通过访问resttemplate 里面的controller方法,就可以操作到 eruekaclient 工程里的数据了,实现了模块间的调用
查询:
添加:
修改:
删除: