欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

1.在build.grdle加入依賴

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

2.在config包下創(chuàng)建一個(gè)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)建一個(gè)新的包

“JuHe”,然后包里創(chuàng)建一個(gè)新的類: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)建一個(gè)

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

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值宏定義一個(gè)名字(Constants類下)
        String url="http://v.juhe.cn/toutiao/index?type="+type+"&key=" + Constants.JUHE_KEY;
                                                    //將Jason格式轉(zhuǎn)換為對(duì)象(增強(qiáng)可讀性、易于后期數(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)用接口,驗(yàn)證是否成功

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

相關(guān)文章

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

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

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

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

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

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

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

    JavaScript的基本類型值-String類型

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

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

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

    Flink JobGraph生成源碼解析

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

    Spring中@RequestMapping、@RestController和Postman

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

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

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

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

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

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

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

最新評(píng)論