" />

最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解SpringBoot通過restTemplate實現(xiàn)消費服務(wù)

 更新時間:2022年02月17日 16:14:02   作者:牛奮lch  
本篇文章主要介紹了詳解使用RestTemplate消費spring boot的Restful服務(wù),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

一、RestTemplate說明

RestTemplate是Spring提供的用于訪問Rest服務(wù)的客戶端,RestTemplate提供了多種便捷訪問遠程Http服務(wù)的方法,能夠大大提高客戶端的編寫效率。前面的博客中//www.fzitv.net/article/132885.htm,已經(jīng)使用Jersey客戶端來實現(xiàn)了消費spring boot的Restful服務(wù),接下來,我們使用RestTemplate來消費前面示例中的Restful服務(wù),前面的示例:
springboot整合H2內(nèi)存數(shù)據(jù)庫,實現(xiàn)單元測試與數(shù)據(jù)庫無關(guān)性

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

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

二、創(chuàng)建工程

三、工程結(jié)構(gòu)

pom文件依賴如下:

<?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> 
    <!-- 熱啟動,熱部署依賴包,為了調(diào)試方便,加入此包 --> 
    <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調(diào)用Restful服務(wù)后,需要將對應(yīng)的json串轉(zhuǎn)換成User對象,所以需要將這個類拷貝到該工程中,如下:

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 
 * 創(chuàng)建時間:2017-1-20 下午2:05:45 
 * @return 
 */ 
@Override 
public String toString() { 
  return "User [id=" + id + ", username=" + username + ", name=" + name 
      + ", age=" + age + ", balance=" + balance + "]"; 
} 
} 

五,編寫controller

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/是前面服務(wù)的對應(yīng)的url 
        User u = this.restTemplate.getForObject("http://localhost:7900/user/" + id, 
        User.class); 
    System.out.println(u); 
    return u; 
  } 
} 

六、啟動程序

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默認已經(jīng)注入了RestTemplateBuilder實例 
  @Bean 
  public RestTemplate restTemplate() { 
    return builder.build(); 
  } 
 
  public static void main(String[] args) { 
    SpringApplication.run(SpringbootRestTemplateApplication.class, args); 
  } 
} 

七、測試

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

測試結(jié)果如下:

控制臺打印結(jié)果:

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

通過上面的測試,說明我們已經(jīng)成功的調(diào)用了spring boot的Restful服務(wù)。

八、改進

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

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

此處出現(xiàn)了硬編碼,當(dāng)服務(wù)器地址改變的時候,需要改動對應(yīng)的代碼,改進的方法,將Restful服務(wù)的地址寫到配置文件中。
修改controller如下:

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服務(wù)對應(yīng)的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; 
  } 
} 

配置文件修改如下:

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

啟動程序:

發(fā)現(xiàn)測試是ok的,后面我們會引入spring cloud對這種調(diào)用方式進行進一步的改進!

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Netty分布式ByteBuf使用的底層實現(xiàn)方式源碼解析

    Netty分布式ByteBuf使用的底層實現(xiàn)方式源碼解析

    這篇文章主要為大家介紹了Netty分布式ByteBuf使用底層實現(xiàn)方式源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-03-03
  • Java騷操作之CountDownLatch代碼詳解

    Java騷操作之CountDownLatch代碼詳解

    這篇文章主要介紹了Java騷操作之CountDownLatch代碼詳解,本文通過實例圖文相結(jié)合給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-02-02
  • SpringBoot調(diào)用第三方WebService接口的兩種方法

    SpringBoot調(diào)用第三方WebService接口的兩種方法

    本文主要介紹了SpringBoot調(diào)用第三方WebService接口的兩種方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • JNDI在JavaEE中的角色_動力節(jié)點Java學(xué)院整理

    JNDI在JavaEE中的角色_動力節(jié)點Java學(xué)院整理

    這篇文章主要介紹了JNDI在JavaEE中的角色,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08
  • 淺談SpringBoot中的@Conditional注解的使用

    淺談SpringBoot中的@Conditional注解的使用

    這篇文章主要介紹了淺談SpringBoot中的@Conditional注解的使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-04-04
  • 從內(nèi)存地址解析Java的static關(guān)鍵字的作用

    從內(nèi)存地址解析Java的static關(guān)鍵字的作用

    這篇文章主要介紹了從內(nèi)存地址解析Java的static關(guān)鍵字的作用,包括靜態(tài)成員變量和靜態(tài)方法等重要內(nèi)容,需要的朋友可以參考下
    2015-10-10
  • Java中的 FilterInputStream簡介_動力節(jié)點Java學(xué)院整理

    Java中的 FilterInputStream簡介_動力節(jié)點Java學(xué)院整理

    FilterInputStream 的作用是用來“封裝其它的輸入流,并為它們提供額外的功能”。接下來通過本文給大家分享Java中的 FilterInputStream簡介,感興趣的朋友一起學(xué)習(xí)吧
    2017-05-05
  • Java經(jīng)典設(shè)計模式之適配器模式原理與用法詳解

    Java經(jīng)典設(shè)計模式之適配器模式原理與用法詳解

    這篇文章主要介紹了Java經(jīng)典設(shè)計模式之適配器模式,簡單說明了適配器模式的概念、原理,并結(jié)合實例形式分析了java適配器模式的用法與相關(guān)注意事項,需要的朋友可以參考下
    2017-08-08
  • Java中的main函數(shù)的詳細介紹

    Java中的main函數(shù)的詳細介紹

    這篇文章主要介紹了Java中的main函數(shù)的詳細介紹的相關(guān)資料,main()函數(shù)在java程序中必出現(xiàn)的函數(shù),這里就講解下使用方法,需要的朋友可以參考下
    2017-09-09
  • SpringBoot整合Javamail實現(xiàn)郵件發(fā)送功能

    SpringBoot整合Javamail實現(xiàn)郵件發(fā)送功能

    郵件發(fā)送是一個很普遍的功能,springboot整合了相關(guān)的starter,本文給大家介紹了可以實現(xiàn)一個簡單的郵件發(fā)送功能的實例,文中通過代碼給大家介紹的非常詳細,感興趣的朋友可以參考下
    2023-12-12

最新評論

凌海市| 武宁县| 樟树市| 绥阳县| 南雄市| 抚宁县| 突泉县| 扶绥县| 平昌县| 朔州市| 农安县| 武宁县| 广南县| 黑水县| 垫江县| 商丘市| 芮城县| 桦南县| 托克逊县| 武定县| 新龙县| 武安市| 黄石市| 如东县| 双江| 屏东市| 安图县| 留坝县| 望奎县| 许昌市| 拜城县| 云安县| 张家口市| 游戏| 阿尔山市| 德阳市| 平罗县| 曲阜市| 富蕴县| 高雄市| 托克逊县|