李锋镝的博客 - LiFengdi.Com

  • 首页
  • 时间轴
  • 留言
  • 左邻右舍
  • 我的日常
  • 关于我
寄蜉蝣于天地 渺沧海之一粟
挫其锐 解其纷 和其光 同其尘
  1. 首页
  2. 转载
  3. 技术
  4. 正文

SpringBoot使用RestTemplate进行接口调用

2021年1月19日 18821点热度 1人点赞 0条评论

引入SpringBoot本来就支持REST服务,因此在SpringBoot项目之间经常需要通过REST的形式来相互调用。本文以具体的实例带大家了解一下SpringBoot中怎么使用RestTemplate进行接口调用。同时,会讲解部分相关源码。

RestTemplate的实例化

RestTemplate实例通常需要自己进行定制,SpringBoot相关的自动配置bean。但是,SpringBoot提供了自动配置的RestTemplateBuilder,可以用它来创建RestTemplate实例。

那么,SpringBoot是如何提供RestTemplateBuilder的呢?我们先来看一下源代码。自动配置的源码位于spring-boot-autoconfigure的RestTemplateAutoConfiguration当中。看一下部分源码:

@Configuration(
    proxyBeanMethods = false
)
@AutoConfigureAfter({HttpMessageConvertersAutoConfiguration.class})
@ConditionalOnClass({RestTemplate.class})
@Conditional({RestTemplateAutoConfiguration.NotReactiveWebApplicationCondition.class})
public class RestTemplateAutoConfiguration {

    @Bean
    @Lazy
    @ConditionalOnMissingBean
    public RestTemplateBuilder restTemplateBuilder(RestTemplateBuilderConfigurer restTemplateBuilderConfigurer) {
        RestTemplateBuilder builder = new RestTemplateBuilder(new RestTemplateCustomizer[0]);
        return restTemplateBuilderConfigurer.configure(builder);
    }

    // 省略其他代码
}

通过上面代码可以看出springboot默认会自动实例化这么一个RestTemplateBuilder,并且bean的名称为restTemplateBuilder。那么,此时我们就可以直接注入来进行使用了。

RestTemplate的实例化配置,有两种方案可选,方案一:

@Configuration
public class RestTemplateConfig {

    @Autowired
    private RestTemplateBuilder restTemplateBuilder;

    @Bean
    public RestTemplate restTemplate() {
        restTemplateBuilder.setConnectTimeout(Duration.ofSeconds(5));
        restTemplateBuilder.setReadTimeout(Duration.ofSeconds(5));
        return restTemplateBuilder.build();
    }
}

此种方案很直观明白,直接注入RestTemplateBuilder,然后在实例化RestTemplate时进行使用即可。

另外一种方案,更简化一些。直接作为restTemplate方法的参数传入,SpringBoot在实例化RestTemplate时会自动匹配注入,使用示例如下:

@Component
public class RestConfig {

    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder builder) {
        builder.setConnectTimeout(Duration.ofSeconds(5));
        builder.setReadTimeout(Duration.ofSeconds(5));
        return builder.build();
    }
}

当然,根据你的具体需要可以调用RestTemplateBuilder的其他方法,进行参数项的设置,也就是定制化你的RestTemplate,最后调用build方法进行构建。

通过上面形式,完成了RestTemplate的初始化操作,在其他地方便可以直接使用了。

RestTemplate的使用

这里在一个项目中先定义个Rest的请求,然后通过RestTemplate进行调用该服务。整体实现代码如下:

@RestController
public class TestController {

    @Resource
    private RestTemplate restTemplate;

    @GetMapping("/hello")
    public String hello() {
        return "Hello world!";
    }

    @GetMapping("/testHello")
    public void testHello() {
        String url = "http://127.0.0.1:8080/hello";
        ResponseEntity<String> result = restTemplate.getForEntity(url, String.class);
        System.out.println(result.getBody());
    }
}

其中/hello提供被调用的服务,通过/testHello来进行调用。启动项目,访问/testHello,控制台便会打出“Hello world!”,说明调用成功。

当然RestTemplate来提供了各类请求的支持,比如getForObject、execute、exchange、patchForObject等方法,而且这些方法还都有大量的重载方法,可根据具体的需要进行使用。

小结

其实RestTemplate的使用还是比较简单的,配置实例化之后就可以直接使用了。而且又不需要引入其他依赖,只需引入最基本的spring-boot-starter-web即可。赶紧尝试一下吧,也别忘了根据具体的业务场景修改RestTemplate的定制化及对应方法的使用。

除非注明,否则均为李锋镝的博客 - LiFengdi.Com原创文章,转载必须以链接形式标明本文链接
本文链接:https://www.lifengdi.com/archives/transport/2370
本作品采用 知识共享署名 4.0 国际许可协议 进行许可
标签: JAVA RestTemplate SpringBoot
最后更新:2021年1月19日

李锋镝

既然选择了远方,便只顾风雨兼程。

打赏 点赞
< 上一篇
下一篇 >
guest
您的姓名(必填)
您的邮箱(必填)
您的站点
guest
您的姓名(必填)
您的邮箱(必填)
您的站点
0 评论
Inline Feedbacks
查看所有评论
我的
我的淘宝精品店

山有木兮木有枝,心悦君兮君不知。

最新 热点 随机
最新 热点 随机
《人生海海》读后感 人生天地间,忽如远行客。 九月你好 知足常足,知止常止。 笑一笑吧 总会过去的 眨眼立秋就过了
今天,是我的第三十一个生日阳了...开工啦~一眨眼就三年了……小记hnswlib installation failed
妹妹的画【2019.07.05】 分布式服务生成唯一不重复ID(24位字符串) RabbitMQ Exchange RocketMQ入门级教程 居住证可算是申请通过了…… 为什么 K8s 在阿里能成功?| 问底中国 IT 技术演进
最近评论
游戏百科 发布于 2 周前(09月17日) 谢谢分享
电商系统开发公司 发布于 3 周前(09月16日) 感谢分享
b2b系统 发布于 3 周前(09月15日) 三年说长也不长
博客录 发布于 3 周前(09月12日) 十分赞同。
放下了 发布于 4 周前(09月05日) 放下了 再见了
有情链接
  • 志文工作室
  • 临窗旋墨
  • 旧时繁华
  • 强仔博客
  • 林三随笔
  • 徐艺扬的博客
  • 云辰博客
  • 韩小韩博客
  • 知向前端
  • 林羽凡
  • 情侣头像
  • 哥斯拉
  • 博客录

COPYRIGHT © 2022 lifengdi.com. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

豫ICP备16004681号-2