SpringBoot-RestTemplate实现调用第三方API的方式

 更新时间:2022年12月05日 10:21:49   作者:David在学习  
RestTemplate 是由 Spring 提供的一个 HTTP 请求工具,它提供了常见的REST请求方案的模版,例如 GET 请求、POST 请求、PUT 请求、DELETE 请求以及一些通用的请求执行方法 exchange 以及 execute,下面看下SpringBoot RestTemplate调用第三方API的方式

RestTemplate简介
Spring RestTemplate 是 Spring 提供的用于访问 Rest 服务的客户端,RestTemplate 提供了多种便捷访问远程Http服务的方法,能够大大提高客户端的编写效率,所以很多客户端比如 Android或者第三方服务商都是使用 RestTemplate 请求 restful 服务。

下面通过代码讲解下SpringBoot-RestTemplate实现调用第三方API的方法,内容如下所示:

1. RestTemplate的方式来调用别人的API,将数据转化为json 格式,引入了fastjson

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.28</version>
</dependency>

2. 编写RestTemlateConfig,配置好相关信息

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,调用第三方的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;
    }
}

到此这篇关于SpringBoot-RestTemplate实现调用第三方API的方式的文章就介绍到这了,更多相关SpringBoot RestTemplate调用第三方API内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • @Scheduled fixedDelayString 加载properties配置方式

    @Scheduled fixedDelayString 加载properties配置方式

    这篇文章主要介绍了@Scheduled fixedDelayString 加载properties配置方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-10-10
  • SpringBoot如何集成Kafka低版本和高版本

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

    这篇文章主要介绍了SpringBoot如何集成Kafka低版本和高版本问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-03-03
  • Java中的static静态代码块的使用详解

    Java中的static静态代码块的使用详解

    本篇文章介绍了,Java中的static静态代码块的使用详解。需要的朋友参考下
    2013-04-04
  • SpringBoot实现登录校验(JWT令牌)

    SpringBoot实现登录校验(JWT令牌)

    JWT全称为JSON Web Token,是一种用于身份验证的开放标准,本文主要介绍了SpringBoot实现登录校验(JWT令牌),具有一定的参考价值,感兴趣的可以了解一下
    2023-12-12
  • 详解jeefast和Mybatis实现二级联动的问题

    详解jeefast和Mybatis实现二级联动的问题

    这篇文章主要介绍了详解jeefast和Mybatis实现二级联动的问题,本文通过图文实例相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-10-10
  • MyBatis注解CRUD与执行流程深入探究

    MyBatis注解CRUD与执行流程深入探究

    这篇文章主要介绍了MyBatis注解CRUD与执行流程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧
    2023-02-02
  • Java静态代理和动态代理详解

    Java静态代理和动态代理详解

    这篇文章主要介绍了Java静态代理和动态代理,本文通过代码示例给大家讲解的非常详细,对大家的学习或工作有一定的帮助,需要的朋友可以参考下
    2024-11-11
  • Struts2之Validator验证框架的详细介绍

    Struts2之Validator验证框架的详细介绍

    Struts2中提供了数据校验验证数据例如验证邮件、数字等,本篇文章介绍了Struts2之Validator的详细介绍,有兴趣的可以了解一下。
    2017-03-03
  • 踩坑Debug启动失败,无报错信息问题

    踩坑Debug启动失败,无报错信息问题

    在进行项目debug时遇到了无法启动的问题,项目一直处于正在启动状态,但未出现任何报错信息,分析原因可能是存在不合法的断点位置,即断点未打在方法内部,解决方法是检查所有断点信息,并移除非法断点,之后项目能够正常启动
    2023-02-02
  • springboot如何通过controller层实现页面切换

    springboot如何通过controller层实现页面切换

    在Spring Boot中,通过Controller层实现页面切换背景,Spring Boot的默认注解是@RestController,它包含了@Controller和@ResponseBody,@ResponseBody会将返回值转换为字符串返回,因此无法实现页面切换,将@RestController换成@Controller
    2024-12-12

最新评论