最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2022年12月05日 10:21:49   作者:David在學(xué)習(xí)  
RestTemplate?是由?Spring?提供的一個(gè)?HTTP?請(qǐng)求工具,它提供了常見的REST請(qǐng)求方案的模版,例如?GET?請(qǐng)求、POST?請(qǐng)求、PUT?請(qǐng)求、DELETE?請(qǐng)求以及一些通用的請(qǐng)求執(zhí)行方法?exchange?以及?execute,下面看下SpringBoot?RestTemplate調(diào)用第三方API的方式

RestTemplate簡(jiǎn)介
Spring RestTemplate 是 Spring 提供的用于訪問 Rest 服務(wù)的客戶端,RestTemplate 提供了多種便捷訪問遠(yuǎn)程Http服務(wù)的方法,能夠大大提高客戶端的編寫效率,所以很多客戶端比如 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. 編寫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)文章

  • @Scheduled fixedDelayString 加載properties配置方式

    @Scheduled fixedDelayString 加載properties配置方式

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

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

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

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

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

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

    JWT全稱為JSON Web Token,是一種用于身份驗(yàn)證的開放標(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)的問題

    詳解jeefast和Mybatis實(shí)現(xiàn)二級(jí)聯(lián)動(dòng)的問題

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

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

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

    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之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ò)信息問題

    踩坑Debug啟動(dòng)失敗,無(wú)報(bào)錯(cuò)信息問題

    在進(jìn)行項(xiàng)目debug時(shí)遇到了無(wú)法啟動(dòng)的問題,項(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è)面切換

    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換成@Controller
    2024-12-12

最新評(píng)論

澄江县| 武汉市| 襄汾县| 许昌县| 连州市| 赣州市| 安阳县| 名山县| 社会| 洞头县| 策勒县| 勃利县| 浮山县| 启东市| 沂南县| 霍林郭勒市| 上高县| 凤凰县| 四会市| 凤凰县| 沐川县| 筠连县| 高青县| 开阳县| 凤凰县| 太和县| 安吉县| 康乐县| 嘉黎县| 巩义市| 嘉黎县| 临猗县| 德州市| 松江区| 宁化县| 安阳县| 万盛区| 伊川县| 蓬莱市| 株洲市| 霞浦县|