一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 詳解SpringBoot通過restTemplate實現消費服務

詳解SpringBoot通過restTemplate實現消費服務

2021-03-22 13:34牛奮lch Java教程

本篇文章主要介紹了詳解使用RestTemplate消費spring boot的Restful服務,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

 

一、RestTemplate說明

 

RestTemplate是Spring提供的用于訪問Rest服務的客戶端,RestTemplate提供了多種便捷訪問遠程Http服務的方法,能夠大大提高客戶端的編寫效率。前面的博客中http://www.ythuaji.com.cn/article/149367.html,已經使用Jersey客戶端來實現了消費spring boot的Restful服務,接下來,我們使用RestTemplate來消費前面示例中的Restful服務,前面的示例:
springboot整合H2內存數據庫,實現單元測試與數據庫無關性

該示例提供的Restful服務如下:http://localhost:7900/user/1 

{"id":1,"username":"user1","name":"張三","age":20,"balance":100.00} 

 

二、創建工程

 

詳解SpringBoot通過restTemplate實現消費服務

 

三、工程結構

 

詳解SpringBoot通過restTemplate實現消費服務

pom文件依賴如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?xml version="1.0" encoding="UTF-8"?>
<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.chhliu.springboot.restful</groupId>
  <artifactId>springboot-rest-template</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
 
  <name>springboot-rest-template</name>
  <description>Demo project for Spring Boot RestTemplate</description>
 
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.3.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>
 
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.7</java.version>
  </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-test</artifactId>
      <scope>test</scope>
    </dependency>
    <!-- 熱啟動,熱部署依賴包,為了調試方便,加入此包 -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-devtools</artifactId>
      <optional>true</optional>
    </dependency>
  </dependencies>
 
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>

 

四、加入vo

 

由于我們使用RestTemplate調用Restful服務后,需要將對應的json串轉換成User對象,所以需要將這個類拷貝到該工程中,如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.chhliu.springboot.restful.vo;
 
import java.math.BigDecimal;
 
public class User {
 private Long id;
 
 private String username;
 
 private String name;
 
 private Short age;
 
 private BigDecimal balance;
 
 // ……省略getter和setter方法
/**
 * attention:
 * Details:TODO
 * @author chhliu
 * 創建時間:2017-1-20 下午2:05:45
 * @return
 */
@Override
public String toString() {
  return "User [id=" + id + ", username=" + username + ", name=" + name
      + ", age=" + age + ", balance=" + balance + "]";
}
}

 

五,編寫controller

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.chhliu.springboot.restful.controller;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
 
import com.chhliu.springboot.restful.vo.User;
 
@RestController
public class RestTemplateController {
  @Autowired
  private RestTemplate restTemplate;
 
  @GetMapping("/template/{id}")
  public User findById(@PathVariable Long id) {
        // http://localhost:7900/user/是前面服務的對應的url
        User u = this.restTemplate.getForObject("http://localhost:7900/user/" + id,
        User.class);
    System.out.println(u);
    return u;
  }
}

 

六、啟動程序

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.chhliu.springboot.restful;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
 
@SpringBootApplication
public class SpringbootRestTemplateApplication {
  // 啟動的時候要注意,由于我們在controller中注入了RestTemplate,所以啟動的時候需要實例化該類的一個實例
  @Autowired
  private RestTemplateBuilder builder;
 
    // 使用RestTemplateBuilder來實例化RestTemplate對象,spring默認已經注入了RestTemplateBuilder實例
  @Bean
  public RestTemplate restTemplate() {
    return builder.build();
  }
 
  public static void main(String[] args) {
    SpringApplication.run(SpringbootRestTemplateApplication.class, args);
  }
}

 

七、測試

 

在瀏覽器中輸入:http://localhost:7902/template/1

測試結果如下:

控制臺打印結果:

User [id=1, username=user1, name=張三, age=20, balance=100.00] 

通過上面的測試,說明我們已經成功的調用了spring boot的Restful服務。

 

八、改進

 

上面的測試中,有一個很不好的地方,

?
1
2
User u = this.restTemplate.getForObject("http://localhost:7900/user/" + id,
        User.class);

此處出現了硬編碼,當服務器地址改變的時候,需要改動對應的代碼,改進的方法,將Restful服務的地址寫到配置文件中。
修改controller如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.chhliu.springboot.restful.controller;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
 
import com.chhliu.springboot.restful.vo.User;
 
@RestController
public class RestTemplateController {
  @Autowired
  private RestTemplate restTemplate;
   
    // Restful服務對應的url地址
  @Value("${user.userServicePath}")
  private String userServicePath;
 
  @GetMapping("/template/{id}")
  public User findById(@PathVariable Long id) {
    User u = this.restTemplate.getForObject(this.userServicePath + id, User.class);
    System.out.println(u);
    return u;
  }
}

配置文件修改如下:

?
1
2
server.port:7902
user.userServicePath=http://localhost:7900/user/

啟動程序:

發現測試是ok的,后面我們會引入spring cloud對這種調用方式進行進一步的改進!

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://blog.csdn.net/liuchuanhong1/article/details/54631080

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 精品免费久久久久久影院 | 99视频精品国在线视频艾草 | 欧美国产日本高清不卡 | 欧美一级片免费 | 国产精品资源站 | 亚洲国产天堂在线观看 | 精品福利视频一区二区三区 | 亚洲美色综合天天久久综合精品 | 十八女下面流水不遮免费 | 亚洲区精品久久一区二区三区 | www.色婷婷.com| 欧美综合在线 | 久久视频精品3线视频在线观看 | 亚洲国产精品网站久久 | 成年男女免费视频网站 | aaa毛片手机在线现看 | 国产经典一区二区三区蜜芽 | 午夜dj免费视频观看社区 | 91看片淫黄大片.在线天堂 | 色综合色狠狠天天综合色 | 亚洲 日本 中文字幕 制服 | 奇米网狠狠网 | 国产rpg迷雾之风冷狐破解 | japanese超丰满人妖 | 纲手被鸣人插 | 青草国内精品视频在线观看 | 短篇最污的乱淫伦小说全集 | 99热这里有免费国产精品 | 肉大捧一进一出视频免费播放 | 四虎影视网址 | 国产午夜精品不卡视频 | 久久中文字幕乱码免费 | 亚洲 欧美 中文 日韩 视频 | 精品综合久久久久久97超人 | 日韩国产欧美一区二区三区 | 涩涩成人| 外女思春台湾三级 | 国产趴着打光屁股sp抽打 | xxxxxx日本处大片免费看 | 日本人做受全过程视频 | 极品丝袜乱系列在线阅读 |