它提供了多种访问远程http服务的方法,是一种简单便捷的访问restful服务模板类,是spring提供的用于访问Rest服务的客户端模板工具集。
1.搭建父模块
1)创建一个maven工程名为rest-template-demo,删除src目录
2)在pom中导入依赖
<properties> <junit.version>4.12</junit.version> <lombok.version>1.16.18</lombok.version> </properties> <!-- 依赖管理,父工程锁定版本--> <dependencyManagement> <dependencies> <!--junit--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> </dependency> <!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.version}</version> </dependency> </dependencies> </dependencyManagement>
2.搭建公共服务模块
公共服务模块用于存放公用的东西,如实体类等。
1)创建maven的工程名为rest-template-demo-api,在pom中导入依赖
<dependencies> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> </dependencies>
2)创建实体类User,内容如下
package com.zxh.entity;
import lombok.*;
@Data
public class User {
private Long id;
private String name;
private String phone;
}
3.搭建服务提供者模块
1)创建maven的工程名为rest-template-demo-provider,在pom中导入依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--引入自己定义的模块-->
<dependency>
<groupId>com.zxh</groupId>
<artifactId>rest-template-demo-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
2)新建启动类ProviderMain8001
package com.zxh; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ProviderMain8001 { public static void main(String[] args) { SpringApplication.run(ProviderMain8001.class, args); } }
3)创建UserController类
package com.zxh.controller;
import com.zxh.entity.User;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("/user")
public class UserController {
@GetMapping("/get/{id}")
public User getUser(@PathVariable("id") long id) {
User user = new User();
user.setId(id);
user.setName("张三");
return user;
}
@PostMapping("/add")
public Map<String, Object> addUser(User user) {
System.out.println(user);
Map<String, Object> map = new HashMap<>();
map.put("msg", "添加成功");
map.put("code", 200);
return map;
}
}
4)在资源目录下创建配置文件application.yml
server: port: 8001
5)运行测试
启动主启动类,使用postman工具输入http://127.0.0.1:8001/user/get/2使用get方式测试,再输入http://127.0.0.1:8001/user/add?name=哈哈哈&phone=101使用post方式测试,均测试通过。
4.搭建服务消费者模块
1)创建maven的工程名为rest-template-demo-consumer,在pom中导入依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--引入自己定义的模块-->
<dependency>
<groupId>com.zxh</groupId>
<artifactId>rest-template-demo-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
2)新建启动类ConsumerMain80
package com.zxh; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ConsumerMain80 { public static void main(String[] args) { SpringApplication.run(ConsumerMain80.class, args); } }
3)创建配置类
package com.zxh.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; //相当于spring中的applicationContext.xml @Configuration public class ConfigBean { @Bean public RestTemplate getRestTemplate(){ return new RestTemplate(); } }
4)创建UserController,调用服务提供者
package com.zxh.controller; import com.zxh.entity.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import org.springframework.web.client.RestTemplate; import java.util.Map; @RestController @RequestMapping("/consumer") public class UserController { private final String BASE_URL = "http://127.0.0.1:8001"; @Autowired private RestTemplate restTemplate; @GetMapping("/get/{id}") public User getUser(@PathVariable("id") long id) { return restTemplate.getForObject(BASE_URL + "/user/get/" + id, User.class); } @PostMapping("/add") public Map<String, Object> addUser(User user) { return restTemplate.postForObject(BASE_URL + "/user/add", user, Map.class); } }
5)在资源目录下创建配置文件application.yml
server: port: 80
6)修改rest-temp-demo-provider的UserController类中addUser方法,给User参数添加@RequestBody注解
7)运行测试
启动主启动类,使用postman工具输入http://127.0.0.1/consumer/get/1使用get方式测试,再输入http://127.0.0.1/consumer/add?name=嘻嘻嘻&phone=101使用post方式测试,均测试通过。
自此,可以看出,使用RestTemplate可以进行服务之间的调用。但是必须指定服务的域名和端口号。