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

Springboot使用RestTemplate調(diào)用第三方接口的操作代碼

 更新時(shí)間:2022年12月05日 10:10:29   作者:技術(shù)經(jīng)理老晶  
這篇文章主要介紹了Springboot使用RestTemplate調(diào)用第三方接口,我只演示了最常使用的請(qǐng)求方式get、post的簡(jiǎn)單使用方法,當(dāng)然RestTemplate的功能還有很多,感興趣的朋友可以參考RestTemplate源碼

前言

工作當(dāng)中,經(jīng)常會(huì)使用到很多第三方提供的功能或者我們自己家也會(huì)提供一些功能給別人使用。
一般都是通過(guò)相互調(diào)用API接口的形式,來(lái)進(jìn)行業(yè)務(wù)功能的對(duì)接。
這里所講的API接口,一般是指HTTP(s)形式的請(qǐng)求,遵循RESTful的標(biāo)準(zhǔn)。
傳統(tǒng)情況下,在Java代碼里面訪問(wèn)restful服務(wù),一般使用Apache的HttpClient,不過(guò)此種方法使用起來(lái)非常繁瑣。
Spring從3.0開(kāi)始提供了一種非常簡(jiǎn)單的模板類(lèi)來(lái)進(jìn)行操作,這就是RestTemplate。

Spring Boot如何使用RestTemplate

1、在項(xiàng)目pom.xml文件中引入web,依賴(lài)內(nèi)容如下:

  <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
  </dependency>

一般項(xiàng)目中都會(huì)已有此依賴(lài)。

2、封裝RestTemplate配置類(lèi)RestTemplateConfig(一般放在api/conf目錄),代碼如下:

package com.***.api.conf;

import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

@Configuration
public class RestTemplateConfig {

    @Bean
    public ClientHttpRequestFactory httpRequestFactory() {
        return new HttpComponentsClientHttpRequestFactory(httpClient());
    }

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate(httpRequestFactory());
    }

    @Bean
    public HttpClient httpClient() {
        Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
                .register("http", PlainConnectionSocketFactory.getSocketFactory())
                .register("https", SSLConnectionSocketFactory.getSocketFactory())
                .build();
        PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry);
        connectionManager.setMaxTotal(3000);
        connectionManager.setDefaultMaxPerRoute(1000);

        RequestConfig requestConfig = RequestConfig.custom()
                .setSocketTimeout(3000)
                .setConnectTimeout(3000)
                .setConnectionRequestTimeout(3000)
                .build();

        return HttpClientBuilder.create()
                .setDefaultRequestConfig(requestConfig)
                .setConnectionManager(connectionManager)
                .build();
    }
}

3、使用舉例
舉例調(diào)用第三方的一個(gè)API接口(POST),根據(jù)手機(jī)號(hào)碼獲取用戶(hù)的個(gè)人信息。

	@Override
    public UserInfo isUserHasCalendar(RestTemplate restTemplate, String sMobile) {
        JSONObject postData = new JSONObject();
        postData.put("mobile", sMobile);
        // URL為第三方HTTP接口地址
        String URL = “******”;
        return restTemplate.postForEntity(URL, postData, UserInfo.class).getBody();
    }

再拿對(duì)接過(guò)的小鵝通API接口,舉例。
小鵝通開(kāi)放API接口文檔:
文檔地址: https://api-doc.xiaoe-tech.com/
1)調(diào)用“獲取access_token”接口:
此接口的官方文檔:

在這里插入圖片描述

請(qǐng)求方式為get,關(guān)鍵代碼如下:

 public String getXiaoETongToken(RestTemplate restTemplate) {
        String url = "https://api.xiaoe-tech.com/token?app_id=app******&client_id=xop******&secret_key=******&grant_type=client_******";
        ResponseEntity<JSONObject> results = restTemplate.exchange(url, HttpMethod.GET, null, JSONObject.class);
        Token tokenDto = JSONObject.toJavaObject(results.getBody(), Token.class);
        return tokenDto.getData().getAccess_token();
    }

2)調(diào)用“查詢(xún)單個(gè)用戶(hù)信息”接口
此接口的官方文檔:

在這里插入圖片描述

請(qǐng)求方式為post,通過(guò)user_id獲取用戶(hù)信息的關(guān)鍵代碼如下:

public XiaoETongUser getXiaoETongUser(RestTemplate restTemplate, String Token, String userId) {
        XiaoETongUser user = null;
        try {
            String url = "https://api.xiaoe-tech.com/xe.user.info.get/1.0.0";
            JSONObject postData = new JSONObject();
            postData.put("access_token", Token);
            postData.put("user_id", userId);
            Map map = new HashMap();
            String[] list = {"name", "nickname", "wx_union_id", "wx_open_id", "wx_app_open_id", "wx_avatar"};
            map.put("field_list", list);
            postData.put("data", map);
            ResultMap resultMap = restTemplate.postForEntity(url, postData, ResultMap.class).getBody();
            if (resultMap.get("code").equals(0)) {
                String data = JSON.toJSONString(resultMap.get("data"));
                user = JSONObject.parseObject(data, XiaoETongUser.class);
            }
        } catch (RestClientException e) {
            e.printStackTrace();
        }
        return user;
    }

總結(jié)

上面,我只演示了最常使用的請(qǐng)求方式get、post的簡(jiǎn)單使用方法,當(dāng)然RestTemplate的功能還有很多,比如:請(qǐng)求方式為delete、put等等,我就不一一舉例了,感興趣的可以去看看RestTemplate的源碼。

到此這篇關(guān)于Springboot使用RestTemplate調(diào)用第三方接口的文章就介紹到這了,更多相關(guān)RestTemplate調(diào)用第三方接口內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java實(shí)現(xiàn)多個(gè)文檔合并

    Java實(shí)現(xiàn)多個(gè)文檔合并

    這篇文章主要為大家詳細(xì)介紹了如何使用?Java?實(shí)現(xiàn)一個(gè)簡(jiǎn)單且通用的文檔合并工具,主要針對(duì)文本文件的合并,感興趣的小伙伴可以參考一下
    2025-03-03
  • JNI語(yǔ)言基本知識(shí)

    JNI語(yǔ)言基本知識(shí)

    JNI是Java Native Interface的縮寫(xiě),它提供了若干的API實(shí)現(xiàn)了Java和其他語(yǔ)言的通信(主要是C&C++)。接下來(lái)通過(guò)本文給大家分享jni 基礎(chǔ)知識(shí),感興趣的朋友一起看看吧
    2017-10-10
  • Mybatis Order by動(dòng)態(tài)參數(shù)防注入方式

    Mybatis Order by動(dòng)態(tài)參數(shù)防注入方式

    這篇文章主要介紹了Mybatis Order by動(dòng)態(tài)參數(shù)防注入方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • JavaEE SpringMVC 請(qǐng)求傳參的示例詳解

    JavaEE SpringMVC 請(qǐng)求傳參的示例詳解

    這篇文章詳細(xì)介紹了SpringMVC中請(qǐng)求參數(shù)的傳遞和接收方式,包括傳遞單個(gè)參數(shù)、多個(gè)參數(shù)、對(duì)象參數(shù)、數(shù)組參數(shù)、集合參數(shù)以及JSON數(shù)據(jù)的傳遞和處理,感興趣的朋友一起看看吧
    2025-02-02
  • 基于redis setIfAbsent的使用說(shuō)明

    基于redis setIfAbsent的使用說(shuō)明

    這篇文章主要介紹了基于redis setIfAbsent的使用說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-01-01
  • Spring?RestTemplate遠(yuǎn)程調(diào)用過(guò)程

    Spring?RestTemplate遠(yuǎn)程調(diào)用過(guò)程

    這篇文章主要介紹了Spring?RestTemplate遠(yuǎn)程調(diào)用過(guò)程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • SpringBoot實(shí)現(xiàn)任意位置獲取HttpServletRequest對(duì)象

    SpringBoot實(shí)現(xiàn)任意位置獲取HttpServletRequest對(duì)象

    這篇文章主要介紹了SpringBoot實(shí)現(xiàn)任意位置獲取HttpServletRequest對(duì)象,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • spring-boot-starter-security的簡(jiǎn)單使用方式

    spring-boot-starter-security的簡(jiǎn)單使用方式

    文章介紹了三種使用Spring Boot Security的方法:基于配置文件、基于配置類(lèi)和基于注解的方式,通過(guò)這些方法,可以實(shí)現(xiàn)對(duì)Web應(yīng)用的權(quán)限控制,確保只有授權(quán)用戶(hù)才能訪問(wèn)特定資源
    2024-11-11
  • java正則表達(dá)式如何獲取xml文件中指定節(jié)點(diǎn)的值

    java正則表達(dá)式如何獲取xml文件中指定節(jié)點(diǎn)的值

    這篇文章主要介紹了java正則表達(dá)式如何獲取xml文件中指定節(jié)點(diǎn)的值問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • rocketmq消費(fèi)負(fù)載均衡--push消費(fèi)詳解

    rocketmq消費(fèi)負(fù)載均衡--push消費(fèi)詳解

    這篇文章主要介紹了rocketmq消費(fèi)負(fù)載均衡--push消費(fèi)詳解,本文介紹了DefaultMQPushConsumerImpl消費(fèi)者,客戶(hù)端負(fù)載均衡相關(guān)知識(shí)點(diǎn)。,需要的朋友可以參考下
    2019-06-06

最新評(píng)論

栾川县| 界首市| 资源县| 临猗县| 镇平县| 天长市| 丹东市| 青龙| 西乡县| 临汾市| 庆阳市| 广西| 离岛区| 江油市| 宿迁市| 澄城县| 祥云县| 偃师市| 平泉县| 平乐县| 长治县| 稻城县| 扎鲁特旗| 台中市| 荣成市| 尉犁县| 祁阳县| 罗城| 彭泽县| 祁连县| 连平县| 安新县| 舞阳县| 万源市| 孙吴县| 红安县| 雷波县| 合水县| 澎湖县| 库伦旗| 双鸭山市|