亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

RestTemplate在Spring或非Spring環(huán)境下使用精講

 更新時(shí)間:2022年03月16日 16:56:49   作者:字母哥哥  
這篇文章主要為大家介紹了RestTemplate在Spring或非Spring環(huán)境下使用精講,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

一、什么是 RestTemplate?

RestTemplate是執(zhí)行HTTP請(qǐng)求的同步阻塞式的客戶端,它在HTTP客戶端庫(kù)(例如JDK HttpURLConnection,Apache HttpComponents,okHttp等)基礎(chǔ)封裝了更加簡(jiǎn)單易用的模板方法API。也就是說(shuō)RestTemplate是一個(gè)封裝,底層的實(shí)現(xiàn)還是java應(yīng)用開發(fā)中常用的一些HTTP客戶端。但是相對(duì)于直接使用底層的HTTP客戶端庫(kù),它的操作更加方便、快捷,能很大程度上提升我們的開發(fā)效率。

RestTemplate作為spring-web項(xiàng)目的一部分,在Spring 3.0版本開始被引入。RestTemplate類通過(guò)為HTTP方法(例如GET,POST,PUT,DELETE等)提供重載的方法,提供了一種非常方便的方法訪問(wèn)基于HTTP的Web服務(wù)。如果你的Web服務(wù)API基于標(biāo)準(zhǔn)的RESTful風(fēng)格設(shè)計(jì),使用效果將更加的完美。

根據(jù)Spring官方文檔及源碼中的介紹,RestTemplate在將來(lái)的版本中它可能會(huì)被棄用,因?yàn)樗麄円言赟pring 5中引入了WebClient作為非阻塞式Reactive HTTP客戶端。但是RestTemplate目前在Spring 社區(qū)內(nèi)還是很多項(xiàng)目的“重度依賴”,比如說(shuō)Spring Cloud。另外,RestTemplate說(shuō)白了是一個(gè)客戶端API封裝,和服務(wù)端相比,非阻塞Reactive 編程的需求并沒(méi)有那么高。

RestTemplate在將來(lái)的版本中它可能會(huì)被棄用

二、非Spring環(huán)境下使用RestTemplate

為了方便后續(xù)開發(fā)測(cè)試,首先介紹一個(gè)網(wǎng)站給大家。JSONPlaceholder是一個(gè)提供免費(fèi)的在線REST API的網(wǎng)站,我們?cè)陂_發(fā)時(shí)可以使用它提供的url地址測(cè)試下網(wǎng)絡(luò)請(qǐng)求以及請(qǐng)求參數(shù)?;蛘弋?dāng)我們程序需要獲取一些模擬數(shù)據(jù)、模擬圖片時(shí)也可以使用它。

RestTemplate是spring的一個(gè)rest客戶端,在spring-web這個(gè)包下。這個(gè)包雖然叫做spring-web,但是它的RestTemplate可以脫離Spring 環(huán)境使用。

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-web</artifactId>
  <version>5.2.6.RELEASE</version>
</dependency>

測(cè)試一下Hello world,使用RestTemplate發(fā)送一個(gè)GET請(qǐng)求,并把請(qǐng)求得到的JSON數(shù)據(jù)結(jié)果打印出來(lái)。

@Test
public void simpleTest()
{
    RestTemplate restTemplate = new RestTemplate();
    String url = "http://jsonplaceholder.typicode.com/posts/1";
    String str = restTemplate.getForObject(url, String.class);
    System.out.println(str);
}

服務(wù)端是JSONPlaceholder網(wǎng)站,幫我們提供的服務(wù)端API。需要注意的是:"http://jsonplaceholder.typicode.com/posts/1"服務(wù)URL,雖然URL里面有posts這個(gè)單詞,但是它的英文含義是:帖子或者公告,而不是我們的HTTP Post協(xié)議。

所以說(shuō)"http://jsonplaceholder.typicode.com/posts/1",請(qǐng)求的數(shù)據(jù)是:id為1的Post公告資源。打印結(jié)果如下:

jsonplaceholder

這里我們只是演示了RestTemplate 最基礎(chǔ)的用法,RestTemplate 會(huì)寫成一個(gè)系列的文章,請(qǐng)大家關(guān)注。

三、Spring環(huán)境下使用RestTemplate

將maven坐標(biāo)從spring-web換成spring-boot-starter-web

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

將RestTemplate配置初始化為一個(gè)Bean。這種初始化方法,是使用了JDK 自帶的HttpURLConnection作為底層HTTP客戶端實(shí)現(xiàn)。我們還可以把底層實(shí)現(xiàn)切換為Apache HttpComponents,okHttp等,我們后續(xù)章節(jié)會(huì)為大家介紹。

@Configuration
public class ContextConfig {
    //默認(rèn)使用JDK 自帶的HttpURLConnection作為底層實(shí)現(xiàn)
    @Bean
    public RestTemplate restTemplate(){
        RestTemplate restTemplate = new RestTemplate();
        return restTemplate;
    }
}

在需要使用RestTemplate 的位置,注入并使用即可。

  @Resource //@AutoWired
  private RestTemplate restTemplate;

以上就是RestTemplate在Spring或非Spring環(huán)境下使用精講的詳細(xì)內(nèi)容,更多關(guān)于RestTemplate在Spring環(huán)境下中使用的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論