SpringBoot-RestTemplate实现调用第三方API的方式
作者:David在学习 时间:2022-12-29 09:49:56
RestTemplate简介
Spring RestTemplate 是 Spring 提供的用于访问 Rest 服务的客户端,RestTemplate 提供了多种便捷访问远程Http服务的方法,能够大大提高客户端的编写效率,所以很多客户端比如 Android或者第三方服务商都是使用 RestTemplate 请求 restful 服务。
下面通过代码讲解下SpringBoot-RestTemplate实现调用第三方API的方法,内容如下所示:
1. RestTemplate的方式来调用别人的API,将数据转化为json 格式,引入了fastjson
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.28</version>
</dependency>
2. 编写RestTemlateConfig,配置好相关信息
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.编写controller,调用第三方的API,浏览器模拟get请求,postman模拟post请求
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.Map;
@RestController
public class SpringRestTemplateController {
@Autowired
private RestTemplate restTemplate;
/***********HTTP GET method*************/
@GetMapping("/testGetApi")
public String getJson(){
String url="http://localhost:8089/o2o/getshopbyid?shopId=19";
//String json =restTemplate.getForObject(url,Object.class);
ResponseEntity<String> results = restTemplate.exchange(url, HttpMethod.GET, null, String.class);
String json = results.getBody();
return json;
}
/**********HTTP POST method**************/
@PostMapping(value = "/testPost")
public Object postJson(@RequestBody JSONObject param) {
System.out.println(param.toJSONString());
param.put("action", "post");
param.put("username", "tester");
param.put("pwd", "123456748");
return param;
}
@PostMapping(value = "/testPostApi")
public Object testPost() {
String url = "http://localhost:8081/girl/testPost";
JSONObject postData = new JSONObject();
postData.put("descp", "request for post");
JSONObject json = restTemplate.postForEntity(url, postData, JSONObject.class).getBody();
return json;
}
}
来源:https://blog.csdn.net/a1032818891/article/details/81172478
标签:SpringBoot,RestTemplate,API
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Spring Boot启动端口修改方法
2022-11-21 19:37:53
详解C#使用AD(Active Directory)验证内网用户名密码
2023-03-03 23:17:47
C#多线程系列之任务基础(一)
2022-12-09 04:48:22
一篇带你解析入门LongAdder源码
2023-11-28 20:17:52
![](https://img.aspxhome.com/file/2023/1/60401_0s.png)
单点登录的三种方式和JWT的介绍与使用
2023-05-19 22:10:59
java自定义异常打印内容详解
2022-03-10 23:58:32
![](https://img.aspxhome.com/file/2023/2/77082_0s.jpg)
java中URLEncoder.encode与URLDecoder.decode处理url特殊参数的方法
2022-05-22 05:32:00
dubbo将异常转换成RuntimeException的原因分析 ExceptionFilter
2023-11-24 14:25:13
![](https://img.aspxhome.com/file/2023/3/64143_0s.jpg)
Android使用Xutils3进行断点下载的实例
2021-08-13 21:59:27
利用Android封装一个有趣的Loading组件
2023-01-07 16:25:28
![](https://img.aspxhome.com/file/2023/9/98659_0s.png)
Java对List进行排序的两种实现方法
2022-10-31 13:40:06
![](https://img.aspxhome.com/file/2023/8/65758_0s.png)
MyBatis 多个条件使用Map传递参数进行批量删除方式
2023-11-29 08:09:01
![](https://img.aspxhome.com/file/2023/8/116208_0s.png)
C#中反射和扩展方法如何运用
2023-08-02 01:43:16
zookeeper概述图文详解
2021-12-07 15:46:03
![](https://img.aspxhome.com/file/2023/6/71946_0s.png)
Java 通过API操作GraphQL
2021-12-24 03:21:49
C#多线程之线程池ThreadPool详解
2021-10-30 23:59:45
详解Spring Bean的集合注入和自动装配
2023-02-18 15:02:58
![](https://img.aspxhome.com/file/2023/9/60829_0s.jpg)
SpringBoot整合liquibase及liquibase生成初始化脚本的方式
2023-07-29 11:53:18
![](https://img.aspxhome.com/file/2023/9/91089_0s.png)
C#统计字符串的方法
2021-06-12 17:00:12
详解Java如何在Array和List之间进行转换
2023-05-27 07:37:27
![](https://img.aspxhome.com/file/2023/9/95029_0s.jpg)