spring RestTemplate 工程导入

spring 提供了RestTemplate的工具类对 httpclient,okhttp,Jdk UrlConnection 三中http 客户端封装。

RestTemplate 使用:

 1 @RunWith(SpringRunner.class)
 2 @SpringBootTest
 3 public class DemoRestTemplateTests {
 4 
 5     @Autowired
 6     private RestTemplate restTemplate;
 7 
 8     @Test
 9     public void test(){
10         String url = "http://localhost/user/8";
11         //对json 反序列化
12         User user = restTemplate.getForObject(url, User.class);
13         System.out.println(user.toString());
14     }
15 
16 }

相应的pom.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5     <groupId>com.spring.cloud</groupId>
 6     <artifactId>demo</artifactId>
 7     <version>0.0.1-SNAPSHOT</version>
 8     <name>RestTemplateDemo</name>
 9     <description>Demo project for Spring Boot</description>
10 
11     <parent>
12         <artifactId>spring-boot-starter-parent</artifactId>
13         <groupId>org.springframework.boot</groupId>
14         <version>2.3.5.RELEASE</version>
15     </parent>
16 
17     <properties>
18         <java.version>1.8</java.version>
19         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
21     </properties>
22 
23     <dependencies>
24         <dependency>
25             <groupId>org.springframework.boot</groupId>
26             <artifactId>spring-boot-starter</artifactId>
27         </dependency>
28 
29         <dependency>
30             <groupId>org.springframework.boot</groupId>
31             <artifactId>spring-boot-starter-web</artifactId>
32         </dependency>
33 
34         <dependency>
35             <groupId>org.springframework.boot</groupId>
36             <artifactId>spring-boot-starter-test</artifactId>
37 <!--            <scope>test</scope>-->
38 <!--            <exclusions>-->
39 <!--                <exclusion>-->
40 <!--                    <groupId>org.junit.vintage</groupId>-->
41 <!--                    <artifactId>junit-vintage-engine</artifactId>-->
42 <!--                </exclusion>-->
43 <!--            </exclusions>-->
44         </dependency>
45 
46         <dependency>
47             <groupId>org.projectlombok</groupId>
48             <artifactId>lombok</artifactId>
49         </dependency>
50     </dependencies>
51 </project>

 

posted @ 2020-12-01 11:16  爱,诗意永存  阅读(811)  评论(0编辑  收藏  举报