欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot-RestTemplate實現(xiàn)調(diào)用第三方API的方式

 更新時間:2022年12月05日 10:21:49   作者:David在學(xué)習(xí)  
RestTemplate?是由?Spring?提供的一個?HTTP?請求工具,它提供了常見的REST請求方案的模版,例如?GET?請求、POST?請求、PUT?請求、DELETE?請求以及一些通用的請求執(zhí)行方法?exchange?以及?execute,下面看下SpringBoot?RestTemplate調(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配置方式

    這篇文章主要介紹了@Scheduled fixedDelayString 加載properties配置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • SpringBoot如何集成Kafka低版本和高版本

    SpringBoot如何集成Kafka低版本和高版本

    這篇文章主要介紹了SpringBoot如何集成Kafka低版本和高版本問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • Java中的static靜態(tài)代碼塊的使用詳解

    Java中的static靜態(tài)代碼塊的使用詳解

    本篇文章介紹了,Java中的static靜態(tài)代碼塊的使用詳解。需要的朋友參考下
    2013-04-04
  • SpringBoot實現(xiàn)登錄校驗(JWT令牌)

    SpringBoot實現(xiàn)登錄校驗(JWT令牌)

    JWT全稱為JSON Web Token,是一種用于身份驗證的開放標(biāo)準(zhǔn),本文主要介紹了SpringBoot實現(xiàn)登錄校驗(JWT令牌),具有一定的參考價值,感興趣的可以了解一下
    2023-12-12
  • 詳解jeefast和Mybatis實現(xiàn)二級聯(lián)動的問題

    詳解jeefast和Mybatis實現(xiàn)二級聯(lián)動的問題

    這篇文章主要介紹了詳解jeefast和Mybatis實現(xiàn)二級聯(lián)動的問題,本文通過圖文實例相結(jié)合給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-10-10
  • MyBatis注解CRUD與執(zhí)行流程深入探究

    MyBatis注解CRUD與執(zhí)行流程深入探究

    這篇文章主要介紹了MyBatis注解CRUD與執(zhí)行流程,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2023-02-02
  • Java靜態(tài)代理和動態(tài)代理詳解

    Java靜態(tài)代理和動態(tài)代理詳解

    這篇文章主要介紹了Java靜態(tài)代理和動態(tài)代理,本文通過代碼示例給大家講解的非常詳細,對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2024-11-11
  • Struts2之Validator驗證框架的詳細介紹

    Struts2之Validator驗證框架的詳細介紹

    Struts2中提供了數(shù)據(jù)校驗驗證數(shù)據(jù)例如驗證郵件、數(shù)字等,本篇文章介紹了Struts2之Validator的詳細介紹,有興趣的可以了解一下。
    2017-03-03
  • 踩坑Debug啟動失敗,無報錯信息問題

    踩坑Debug啟動失敗,無報錯信息問題

    在進行項目debug時遇到了無法啟動的問題,項目一直處于正在啟動狀態(tài),但未出現(xiàn)任何報錯信息,分析原因可能是存在不合法的斷點位置,即斷點未打在方法內(nèi)部,解決方法是檢查所有斷點信息,并移除非法斷點,之后項目能夠正常啟動
    2023-02-02
  • springboot如何通過controller層實現(xiàn)頁面切換

    springboot如何通過controller層實現(xiàn)頁面切換

    在Spring Boot中,通過Controller層實現(xiàn)頁面切換背景,Spring Boot的默認注解是@RestController,它包含了@Controller和@ResponseBody,@ResponseBody會將返回值轉(zhuǎn)換為字符串返回,因此無法實現(xiàn)頁面切換,將@RestController換成@Controller
    2024-12-12

最新評論