SpringBoot-RestTemplate實(shí)現(xiàn)調(diào)用第三方API的方式
RestTemplate簡介
Spring RestTemplate 是 Spring 提供的用于訪問 Rest 服務(wù)的客戶端,RestTemplate 提供了多種便捷訪問遠(yuǎn)程Http服務(wù)的方法,能夠大大提高客戶端的編寫效率,所以很多客戶端比如 Android或者第三方服務(wù)商都是使用 RestTemplate 請(qǐng)求 restful 服務(wù)。
下面通過代碼講解下SpringBoot-RestTemplate實(shí)現(xiàn)調(diào)用第三方API的方法,內(nèi)容如下所示:
1. RestTemplate的方式來調(diào)用別人的API,將數(shù)據(jù)轉(zhuǎn)化為json 格式,引入了fastjson
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.28</version> </dependency>
2. 編寫RestTemlateConfig,配置好相關(guān)信息
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,調(diào)用第三方的API,瀏覽器模擬get請(qǐng)求,postman模擬post請(qǐng)求
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; } }
到此這篇關(guān)于SpringBoot-RestTemplate實(shí)現(xiàn)調(diào)用第三方API的方式的文章就介紹到這了,更多相關(guān)SpringBoot RestTemplate調(diào)用第三方API內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringMVC自定義參數(shù)綁定實(shí)現(xiàn)詳解
這篇文章主要介紹了SpringMVC自定義參數(shù)綁定實(shí)現(xiàn)詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11關(guān)于Jedis的用法以及Jedis使用Redis事務(wù)
這篇文章主要介紹了關(guān)于Jedis的用法以及Jedis使用Redis事務(wù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03springboot創(chuàng)建的web項(xiàng)目整合Quartz框架的項(xiàng)目實(shí)踐
本文主要介紹了springboot創(chuàng)建的web項(xiàng)目整合Quartz框架的項(xiàng)目實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06Spring在多線程下保持事務(wù)的一致性的方法實(shí)現(xiàn)
當(dāng)Spring在多線程環(huán)境下運(yùn)行時(shí),確保事務(wù)一致性是非常重要的,本文主要介紹了Spring在多線程下保持事務(wù)的一致性的方法實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-01-01SpringBoot3中數(shù)據(jù)庫集成實(shí)踐詳解
項(xiàng)目工程中,集成數(shù)據(jù)庫實(shí)現(xiàn)對(duì)數(shù)據(jù)的增曬改查管理,是最基礎(chǔ)的能力,所以下面小編就來和大家講講SpringBoot3如何實(shí)現(xiàn)數(shù)據(jù)庫集成,需要的可以參考下2023-08-08SpringBoot+log4j2.xml使用application.yml屬性值問題
這篇文章主要介紹了SpringBoot+log4j2.xml使用application.yml屬性值問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12SpringBoot中的配置文件加載優(yōu)先級(jí)詳解
這篇文章主要介紹了SpringBoot中的配置文件加載優(yōu)先級(jí)詳解,springboot啟動(dòng)會(huì)掃描以下位置的application.properties或者application.yml文件作為Spring?boot的默認(rèn)配置文件,需要的朋友可以參考下2024-01-01Gson中@JsonAdater注解的幾種方式總結(jié)
這篇文章主要介紹了Gson中@JsonAdater注解的幾種方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08