SpringBoot-RestTemplate實現(xiàn)調(diào)用第三方API的方式
RestTemplate簡介
Spring RestTemplate 是 Spring 提供的用于訪問 Rest 服務(wù)的客戶端,RestTemplate 提供了多種便捷訪問遠程Http服務(wù)的方法,能夠大大提高客戶端的編寫效率,所以很多客戶端比如 Android或者第三方服務(wù)商都是使用 RestTemplate 請求 restful 服務(wù)。
下面通過代碼講解下SpringBoot-RestTemplate實現(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請求,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;
}
}
到此這篇關(guān)于SpringBoot-RestTemplate實現(xiàn)調(diào)用第三方API的方式的文章就介紹到這了,更多相關(guān)SpringBoot RestTemplate調(diào)用第三方API內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
@Scheduled fixedDelayString 加載properties配置方式
這篇文章主要介紹了@Scheduled fixedDelayString 加載properties配置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10
詳解jeefast和Mybatis實現(xiàn)二級聯(lián)動的問題
這篇文章主要介紹了詳解jeefast和Mybatis實現(xiàn)二級聯(lián)動的問題,本文通過圖文實例相結(jié)合給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10
springboot如何通過controller層實現(xiàn)頁面切換
在Spring Boot中,通過Controller層實現(xiàn)頁面切換背景,Spring Boot的默認注解是@RestController,它包含了@Controller和@ResponseBody,@ResponseBody會將返回值轉(zhuǎn)換為字符串返回,因此無法實現(xiàn)頁面切換,將@RestController換成@Controller2024-12-12

