Spring Boot(六)RestTemplate
阿新 • • 發佈:2020-12-23
0、準備工作
本文中介紹的工程依賴Spring Boot(五)json格式的快取,我們姑且簡稱它為工程A,所以在啟動本文的專案之前,我們應該先啟動工程A。在啟動工程A成功後,開啟postman,新增一個user,如下圖:
另外我將本文介紹的工程成為工程B,專案A和B的關係就是B會通過http請求拿到A的響應,響應的請求體是一段代表User的json程式碼。
1、建立工程
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.github.ralgond</groupId> <artifactId>boot-resttmp</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.0.RELEASE</version> </parent> <properties> <start-class> com.github.ralgond.bootresttmp.BootResttmpMainApp</start-class> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-json</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.0</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2、工程佈局如下圖
3、編輯application.yml
server:
port: 7777
4、編輯User類
package com.github.ralgond.bootresttmp.entity; public class User { private Integer id; private String name; public User() { } public User(Integer id, String name) { this.id = id; this.name = name; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "User [id=" + id + ", name=" + name + "]"; } }
5、編輯配置類RestTemplateConfig
package com.github.ralgond.bootresttmp; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; @Configuration public class RestTemplateConfig { @Bean RestTemplate restTemplate() { return new RestTemplate(); } }
6、編輯主啟動類
package com.github.ralgond.bootresttmp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import com.github.ralgond.bootresttmp.entity.User;
@SpringBootApplication
@RestController
public class BootResttmpMainApp {
@Autowired
RestTemplate restTemplate;
@RequestMapping(method=RequestMethod.GET, value="/user/{id}")
public User getUser(@PathVariable("id") int id) {
User user = restTemplate.getForObject("http://localhost:8888/user/"+id, User.class);
System.out.println("====>get from remote: "+user);
return user;
}
public static void main(String args[]) {
SpringApplication.run(BootResttmpMainApp.class, args);
}
}