欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring RestTemplate配置及使用

程序员文章站 2022-06-28 11:11:34
...

配置

// 配置类
@Configuration
public class RestTemplateConfiguration {
    @Bean
    public RestTemplate restTemplate(){
        RestTemplate restTemplate = new RestTemplate();
        // 设置字符串转换器编码
        restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
        // 自定义参数转换,解决部分微信api MediaType不匹配问题
        restTemplate.getMessageConverters().add(new WxMappingJackson2HttpMessageConverter());
        // 错误拦截
        restTemplate.setErrorHandler(new DefaultResponseErrorHandler());
        return restTemplate;
    }
    @Bean
    public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        // 自行设置超时时间,一些需要长时间的请求如文件流可以单独配置长一点超时时间
        factory.setReadTimeout(5000);
        factory.setConnectTimeout(15000);
        return factory;
    }
}
// 自定义转换器
public class WxMappingJackson2HttpMessageConverter extends MappingJackson2HttpMessageConverter {
    public WxMappingJackson2HttpMessageConverter() {
        List<MediaType> mediaTypes = new ArrayList<>();
        mediaTypes.add(MediaType.TEXT_PLAIN);
        mediaTypes.add(MediaType.TEXT_HTML); 
        setSupportedMediaTypes(mediaTypes);
    }
}

接口封装

@Component
public class RestTemplateUtil {

    @Autowired
    private RestTemplate restTemplate;

    /**
     * @Author Mark Deng
     * @Description restTemplate 基本调用
     * @Date 13:01 2020/5/19
     * @Param [url, httpMethod, headers, params, c]
     * @Return java.lang.Object
     **/
    public <T> T doRestTemplate(String url, HttpMethod httpMethod, HttpHeaders headers, Object params, Class<T> c) {
        HttpEntity<Object> requestEntity = new HttpEntity<>(params, headers);
        ResponseEntity<T> response = restTemplate.exchange(url, httpMethod, requestEntity, c);
        return response.getBody();
    }

    /**
     * @Author Mark Deng
     * @Description 文件上传
     * @Date 15:34 2020/5/27
     * @Param [url, mediaType, params, c]
     * @Return T
     **/
    public <T> T doRestTemplateFile(String url, MediaType mediaType, Object params, Class<T> c) {
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(mediaType);
        return doRestTemplate(url, HttpMethod.POST, httpHeaders, params, c);
    }

    /**
     * @Author Mark Deng
     * @Description 泛型内对象接收转化问题
     * @Date 13:00 2020/5/19
     * @Param [url, httpMethod, headers, params, c]
     * @Return java.lang.Object
     **/
    public <T> T doRestTemplateT(String url, HttpMethod httpMethod, HttpHeaders headers
            , Object params, ParameterizedTypeReference p) {
        HttpEntity<Object> requestEntity = new HttpEntity<>(params, headers);
        ResponseEntity<T> response = restTemplate.exchange(url, httpMethod, requestEntity, p);
        return response.getBody();
    }

    /**
     * @Author Mark Deng
     * @Description 常用 Json 参数 Post 调用
     * @Date 13:00 2020/5/19
     * @Param [url, params, c]
     * @Return java.lang.Object
     **/
    @Log(value = "RestTemplate远程POST调用API")
    public <T> T doRestTemplatePostJson(String url, Object params, Class<T> c) {
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        return doRestTemplate(url, HttpMethod.POST, httpHeaders, params, c);
    }

    /**
     * @Author Mark Deng
     * @Description 解决泛型内对象转化成 LinkedHashMap 问题
     * @Date 12:58 2020/5/19
     * @Param [url, params, c]
     * @Return java.lang.Object
     **/
    @Log(value = "RestTemplate远程POST调用API")
    public <T> T doRestTemplatePostJsonT(String url, Object params, ParameterizedTypeReference p) {
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        return doRestTemplateT(url, HttpMethod.POST, httpHeaders, params, p);
    }

    /**
     * @Author Mark Deng
     * @Description 常用 Json 参数 Get 调用
     * @Date 12:59 2020/5/19
     * @Param [url, c]
     * @Return java.lang.Object
     **/
    @Log(value = "RestTemplate远程Get调用API")
    public <T> T doRestTemplateGet(String url, Class<T> c) {
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        return doRestTemplate(url, HttpMethod.GET, httpHeaders, null, c);
    }
}