SpringBoot-RestTemplate實(shí)現(xiàn)調(diào)用第三方API的方式
RestTemplate簡(jiǎn)介
Spring RestTemplate 是 Spring 提供的用于訪問(wèn) Rest 服務(wù)的客戶端,RestTemplate 提供了多種便捷訪問(wèn)遠(yuǎn)程Http服務(wù)的方法,能夠大大提高客戶端的編寫(xiě)效率,所以很多客戶端比如 Android或者第三方服務(wù)商都是使用 RestTemplate 請(qǐng)求 restful 服務(wù)。
下面通過(guò)代碼講解下SpringBoot-RestTemplate實(shí)現(xiàn)調(diào)用第三方API的方法,內(nèi)容如下所示:
1. RestTemplate的方式來(lái)調(diào)用別人的API,將數(shù)據(jù)轉(zhuǎn)化為json 格式,引入了fastjson
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.28</version>
</dependency>2. 編寫(xiě)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.編寫(xiě)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)文章
@Scheduled fixedDelayString 加載properties配置方式
這篇文章主要介紹了@Scheduled fixedDelayString 加載properties配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
SpringBoot實(shí)現(xiàn)登錄校驗(yàn)(JWT令牌)
JWT全稱為JSON Web Token,是一種用于身份驗(yàn)證的開(kāi)放標(biāo)準(zhǔn),本文主要介紹了SpringBoot實(shí)現(xiàn)登錄校驗(yàn)(JWT令牌),具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12
詳解jeefast和Mybatis實(shí)現(xiàn)二級(jí)聯(lián)動(dòng)的問(wèn)題
這篇文章主要介紹了詳解jeefast和Mybatis實(shí)現(xiàn)二級(jí)聯(lián)動(dòng)的問(wèn)題,本文通過(guò)圖文實(shí)例相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10
Java靜態(tài)代理和動(dòng)態(tài)代理詳解
這篇文章主要介紹了Java靜態(tài)代理和動(dòng)態(tài)代理,本文通過(guò)代碼示例給大家講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-11-11
Struts2之Validator驗(yàn)證框架的詳細(xì)介紹
Struts2中提供了數(shù)據(jù)校驗(yàn)驗(yàn)證數(shù)據(jù)例如驗(yàn)證郵件、數(shù)字等,本篇文章介紹了Struts2之Validator的詳細(xì)介紹,有興趣的可以了解一下。2017-03-03
踩坑Debug啟動(dòng)失敗,無(wú)報(bào)錯(cuò)信息問(wèn)題
在進(jìn)行項(xiàng)目debug時(shí)遇到了無(wú)法啟動(dòng)的問(wèn)題,項(xiàng)目一直處于正在啟動(dòng)狀態(tài),但未出現(xiàn)任何報(bào)錯(cuò)信息,分析原因可能是存在不合法的斷點(diǎn)位置,即斷點(diǎn)未打在方法內(nèi)部,解決方法是檢查所有斷點(diǎn)信息,并移除非法斷點(diǎn),之后項(xiàng)目能夠正常啟動(dòng)2023-02-02
springboot如何通過(guò)controller層實(shí)現(xiàn)頁(yè)面切換
在Spring Boot中,通過(guò)Controller層實(shí)現(xiàn)頁(yè)面切換背景,Spring Boot的默認(rèn)注解是@RestController,它包含了@Controller和@ResponseBody,@ResponseBody會(huì)將返回值轉(zhuǎn)換為字符串返回,因此無(wú)法實(shí)現(xiàn)頁(yè)面切換,將@RestController換成@Controller2024-12-12

