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)文章
SpringBoot中ApplicationEvent和ApplicationListener用法小結(jié)
這篇文章介紹SpringBoot中ApplicationEvent用法,注意ApplicationEvent和MQ隊列雖然實現(xiàn)的功能相似,但是MQ還是有其不可替代性的,最本質(zhì)的區(qū)別就是MQ可以用于不同系統(tǒng)之間的消息發(fā)布,而SpringEvent這種模式只能在一個系統(tǒng)中,需要的朋友可以參考下2023-03-03
解決spring cloud gateway調(diào)用其他模塊出現(xiàn)的問題
這篇文章主要介紹了解決spring cloud gateway調(diào)用其他模塊出現(xiàn)的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-06-06
IDEA中的yml文件與properties互相轉(zhuǎn)換
這篇文章主要介紹了IDEA中的yml文件與properties互相轉(zhuǎn)換方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10
Java List的remove()方法陷阱以及性能優(yōu)化
Java List在進(jìn)行remove()方法是通常容易踩坑,本文就詳細(xì)的介紹一下陷阱以及性能優(yōu)化,感興趣的可以了解一下2021-10-10
Socket結(jié)合線程池使用實現(xiàn)客戶端和服務(wù)端通信demo
這篇文章主要為大家介紹了Socket結(jié)合線程池的使用來實現(xiàn)客戶端和服務(wù)端通信實戰(zhàn)demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03

