使用RestTemplate调用第三方接口模板

调用第三方接口模板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;

/**
 * 〈请求第三方接口〉
 *
 * @author qsc
 * @create 2019/11/18
 * @since 1.0.0
 */
@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;
    }
}

实现类:

重载模板:

    @Autowired
    private RestTemplate restTemplate;

具体方法为,result为要返回的值

 String result = restTemplate.getForObject(接口地址, String.class);

post方式:

String result = restTemplate.postForObject("*******",OBject, String.class);

 

你可能感兴趣的:(工作)