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; } }
4、另一个项目接收json代码
/** * 接收传递的json参数解析*/ @RequestMapping("/getJson") public String getJson(HttpServletRequest request, HttpServletResponse response) { StringBuffer json = new StringBuffer(); String line = null; try { BufferedReader reader = request.getReader(); while ((line = reader.readLine()) != null) { System.out.println("line:" + line); json.append(line); } } catch (Exception e) { System.out.println(e.toString()); } System.out.println(json.toString()); return json.toString(); }
参考:https://blog.csdn.net/a1032818891/article/details/81172478
分类:
SpringBoot
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
2018-07-30 C# 给一个控件去掉焦点
2018-07-30 C#创建文件夹