SpringBoot-RestTemplate如何实现调用第三方API
目录
- 1.在build.grdle加入依赖
- 2.在config包下创建一个RestTemlateConfig
- 3.在model包下创建一个新的包
- 4.Constans类下将调用接口的AppKey值宏定义
- 5.在controller包下创建一个
- 6.用Postman调用接口,验证是否成功
1.在build.grdle加入依赖
implementation('org.springframework.boot:spring-boot-starter-web')
2.在config包下创建一个RestTemlateConfig 配置好相关信息
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; @Configurationpublic class RestTemplateConfig {@Beanpublic RestTemplate restTemplate(ClientHttpRequestFactory factory){return new RestTemplate(factory); } @Beanpublic ClientHttpRequestFactory simpleClientHttpRequestFactory(){SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory(); factory.setConnectTimeout(15000); factory.setReadTimeout(5000); return factory; }}
3.在model包下创建一个新的包 【SpringBoot-RestTemplate如何实现调用第三方API】“JuHe”,然后包里创建一个新的类:JuheResult 【Art+insert】选择CsonFormat
![SpringBoot-RestTemplate如何实现调用第三方API](https://img.it610.com/image/info11/7e552f26e4bd4245b3e033acacd04948.jpg)
文章图片
4.Constans类下将调用接口的AppKey值宏定义
package com.qiubao.school.api.common; public interface Constants {String JUHE_KEY= "e80611926aa6321048bde9ea73d11190"; }
5.在controller包下创建一个 新的类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 {@Autowiredprivate RestTemplate restTemplate; /***********HTTP GET method*************/@GetMapping("/testGetApi")public JuheResult getJson(@RequestParam(value = "https://www.it610.com/article/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 convertJuhe2Article(JuheResult result) {List 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调用接口,验证是否成功
![SpringBoot-RestTemplate如何实现调用第三方API](https://img.it610.com/image/info11/80e290a45e4e4bc3bceb20c8eb93aa52.jpg)
文章图片
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
推荐阅读
- 考研英语阅读终极解决方案——阅读理解如何巧拿高分
- 如何寻找情感问答App的分析切入点
- 关于QueryWrapper|关于QueryWrapper,实现MybatisPlus多表关联查询方式
- mybatisplus如何在xml的连表查询中使用queryWrapper
- MybatisPlus使用queryWrapper如何实现复杂查询
- python学习之|python学习之 实现QQ自动发送消息
- 孩子不是实现父母欲望的工具——林哈夫
- opencv|opencv C++模板匹配的简单实现
- Node.js中readline模块实现终端输入
- 如何在Mac中的文件选择框中打开系统隐藏文件夹