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

SpringBoot-RestTemplate如何實現(xiàn)調(diào)用第三方API

 更新時間:2021年08月19日 11:32:30   作者:Lukey Alvin  
這篇文章主要介紹了SpringBoot-RestTemplate實現(xiàn)調(diào)用第三方API的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

1.在build.grdle加入依賴

implementation('org.springframework.boot:spring-boot-starter-web')

2.在config包下創(chuàng)建一個RestTemlateConfig

配置好相關(guān)信息

package com.qiubao.school.api.config; 
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
 
@Configuration
public class RestTemplateConfig {
    @Bean
    public RestTemplate restTemplate(ClientHttpRequestFactory factory){
        return new RestTemplate(factory);
    } 
    @Bean
    public ClientHttpRequestFactory simpleClientHttpRequestFactory(){
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setConnectTimeout(15000);
        factory.setReadTimeout(5000);
        return factory;
    }
}

3.在model包下創(chuàng)建一個新的包

“JuHe”,然后包里創(chuàng)建一個新的類:JuheResult 【Art+insert】選擇CsonFormat

4.Constans類下將調(diào)用接口的AppKey值宏定義

package com.qiubao.school.api.common; 
public interface Constants {   
    String JUHE_KEY         = "e80611926aa6321048bde9ea73d11190";
}

5.在controller包下創(chuàng)建一個

新的類SpringRestTemplateController(調(diào)用第三方的API,瀏覽器模擬get請求,postman模擬post請求)

package com.qiubao.school.api.controller; 
import com.alibaba.fastjson.JSONObject;
import com.qiubao.school.api.common.Constants;
import com.qiubao.school.api.model.Article;
import com.qiubao.school.api.model.juhe.JuheResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate; 
import java.util.ArrayList;
import java.util.List;
 
@RestController
@RequestMapping("news")
public class SpringRestTemplateController {
    @Autowired
    private RestTemplate restTemplate;
    /***********HTTP GET method*************/
    @GetMapping("/testGetApi")
    public JuheResult getJson(
            @RequestParam(value = "type", required = false) String type
    ){
        type = StringUtils.isEmpty(type)?"top":type;//定義為可以為空
                                                    // 將調(diào)用接口的Key值宏定義一個名字(Constants類下)
        String url="http://v.juhe.cn/toutiao/index?type="+type+"&key=" + Constants.JUHE_KEY;
                                                    //將Jason格式轉(zhuǎn)換為對象(增強可讀性、易于后期數(shù)據(jù)的更改和增刪)
        JuheResult result = restTemplate.getForObject(url, JuheResult.class);
        return result;
    }
//將調(diào)用的Api的Jason數(shù)據(jù)格式修改為需要的Jason數(shù)據(jù)格式[用到JuheResult方法]
    private List<Article> convertJuhe2Article(JuheResult result) {
        List<Article> articles = new ArrayList<>();
        for (JuheResult.ResultBean.DataBean dataBean:
             result.getResult().getData()) {
            Article article = new Article(); 
 
            article.setUniquekey(dataBean.getUniquekey());
            article.setTitle(dataBean.getTitle());
            article.setAuthor_name(dataBean.getAuthor_name());
            article.setCreateDate(dataBean.getDate());
 
            article.setContent(dataBean.getUrl());
            article.setThumbnail_pic_s(dataBean.getThumbnail_pic_s());
            article.setThumbnail_pic_s02(dataBean.getThumbnail_pic_s02());
            article.setThumbnail_pic_s03(dataBean.getThumbnail_pic_s03());
 
            article.setCategory(dataBean.getCategory());
            article.getCommentCount();
            article.getLikeCount();
            articles.add(article);
        }
        return articles;
    }    
}

6.用Postman調(diào)用接口,驗證是否成功

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • spring整合redis消息監(jiān)聽通知使用的實現(xiàn)示例

    spring整合redis消息監(jiān)聽通知使用的實現(xiàn)示例

    在電商系統(tǒng)中,秒殺,搶購,紅包優(yōu)惠卷等操作,一般都會設(shè)置時間限制,本文主要介紹了spring整合redis消息監(jiān)聽通知使用,具有一定的參考價值,感興趣的可以了解一下
    2021-12-12
  • 詳解在Java中如何優(yōu)雅的停止線程

    詳解在Java中如何優(yōu)雅的停止線程

    線程,作為并發(fā)編程的基礎(chǔ)單元,允許程序同時執(zhí)行多個任務(wù),在Java中,線程可以理解為程序中的獨立執(zhí)行路徑,通過使用線程,開發(fā)者可以創(chuàng)建更加響應(yīng)靈敏、效率更高的應(yīng)用程序,本文小編將給大家介紹一下Java中如何優(yōu)雅的停止線程,需要的朋友可以參考下
    2023-11-11
  • springboot配置文件中敏感數(shù)據(jù)(賬號密碼)加密方式

    springboot配置文件中敏感數(shù)據(jù)(賬號密碼)加密方式

    這篇文章主要介紹了springboot配置文件中敏感數(shù)據(jù)(賬號密碼)加密方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • JavaScript的基本類型值-String類型

    JavaScript的基本類型值-String類型

    String類型用于表示由零或多個16位Unicode字符組成的字符序列,即字符串。在JavaScript中沒有單個的字符型,都是字符串。這篇文章主要介紹了JavaScript的基本類型值String類型,需要的朋友可以參考下
    2017-02-02
  • Java中用內(nèi)存映射處理大文件的實現(xiàn)代碼

    Java中用內(nèi)存映射處理大文件的實現(xiàn)代碼

    下面小編就為大家?guī)硪黄狫ava中用內(nèi)存映射處理大文件的實現(xiàn)代碼。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-06-06
  • Flink JobGraph生成源碼解析

    Flink JobGraph生成源碼解析

    這篇文章主要為大家介紹了Flink JobGraph生成源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-12-12
  • Spring中@RequestMapping、@RestController和Postman

    Spring中@RequestMapping、@RestController和Postman

    本文介紹了Spring框架中常用的@RequestMapping和@RestController注解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-10-10
  • 往DAO類中注入@PersistenceContext和@Resource的區(qū)別詳解

    往DAO類中注入@PersistenceContext和@Resource的區(qū)別詳解

    這篇文章主要介紹了往DAO類中注入@PersistenceContext和@Resource的區(qū)別詳解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • 分享40個Java多線程問題小結(jié)

    分享40個Java多線程問題小結(jié)

    多個線程共存于同一JVM進程里面,所以共用相同的內(nèi)存空間,較之多進程,多線程之間的通信更輕量級,本文給大家分享40個Java多線程問題小結(jié) 的相關(guān)資料,需要的朋友可以參考下
    2015-12-12
  • 使用Spring boot 的profile功能實現(xiàn)多環(huán)境配置自動切換

    使用Spring boot 的profile功能實現(xiàn)多環(huán)境配置自動切換

    這篇文章主要介紹了使用Spring boot 的profile功能實現(xiàn)多環(huán)境配置自動切換的相關(guān)知識,非常不錯,具有一定的參考借鑒價值 ,需要的朋友可以參考下
    2018-11-11

最新評論