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()