如何在项目中引入RestTemplate

本文介绍了RestTemplate的基本概念,如何在项目中集成RestTemplate,以及如何使用它进行GET、POST等REST请求。特别强调了在使用GET请求时,参数需正确拼接到URL中才能确保请求成功。
摘要由CSDN通过智能技术生成

1.什么是 RestTemplate?

RestTemplate 其实是简化了发起 HTTP 请求以及处理响应的过程,并且支持 REST,我们在项目中使用的比较多的可能就是 HttpUtils,使用

2.项目中集成 RestTemplate

首先配置 RestTemplate 的配置类,这个也可以无需配置,但是可在配置中设置连接的超时时间。

package com.jiafly.pisces.common.config;

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;

/**
 * restTemplate配置类
 *
 * @author liuyi
 * @date 2020/1/1 22:35 PM
 */
@Configuration
public class RestTemplateConfig {
   

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

    @Bean
    public ClientHttpRequestFactory simpleClientHttpRequestFactory() 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值