您好,登錄后才能下訂單哦!
本篇內容主要講解“SpringBoot-RestTemplate怎么實現調用第三方API”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“SpringBoot-RestTemplate怎么實現調用第三方API”吧!
1.在build.grdle加入依賴
2.在config包下創建一個RestTemlateConfig
3.在model包下創建一個新的包
4.Constans類下將調用接口的AppKey值宏定義
5.在controller包下創建一個
6.用Postman調用接口,驗證是否成功
implementation('org.springframework.boot:spring-boot-starter-web')
配置好相關信息
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; } }
“JuHe”,然后包里創建一個新的類:JuheResult 【Art+insert】選擇CsonFormat
package com.qiubao.school.api.common; public interface Constants { String JUHE_KEY = "e80611926aa6321048bde9ea73d11190"; }
新的類SpringRestTemplateController(調用第三方的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;//定義為可以為空 // 將調用接口的Key值宏定義一個名字(Constants類下) String url="http://v.juhe.cn/toutiao/index?type="+type+"&key=" + Constants.JUHE_KEY; //將Jason格式轉換為對象(增強可讀性、易于后期數據的更改和增刪) JuheResult result = restTemplate.getForObject(url, JuheResult.class); return result; } //將調用的Api的Jason數據格式修改為需要的Jason數據格式[用到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; } }
到此,相信大家對“SpringBoot-RestTemplate怎么實現調用第三方API”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。