封装一个简单的Http请求工具类

使用了 RestTemplate、Jackson 来封装了一个简单的http请求工具类,方便后续使用

xml 复制代码
<!--jackson依赖 2.11.1 版本-->
<dependency>
	<groupId>com.fasterxml.jackson.core</groupId>
	<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
	<groupId>com.fasterxml.jackson.core</groupId>
	<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
	<groupId>com.fasterxml.jackson.core</groupId>
	<artifactId>jackson-annotations</artifactId>
</dependency>
java 复制代码
/**
 * http工具类
 */
public class HttpUtil {

    /** Rest template */
    private final RestTemplate restTemplate;

    /**
     * Http util
     *
     * @since 1.0.0
     */
    public HttpUtil() {
        this.restTemplate = createRestTemplate();
    }

    /**
     * Http util
     *
     * @param restTemplate rest template
     * @since 1.0.0
     */
    public HttpUtil(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    /**
     * Create rest template
     *
     * @return the rest template
     * @since 1.0.0
     */
    private RestTemplate createRestTemplate() {
        RestTemplate restTemplate = new RestTemplate();
        List<HttpMessageConverter<?>> converters = new ArrayList<>(2);
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        converter.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_JSON, MediaType.APPLICATION_OCTET_STREAM));
        converters.add(converter);
        List<HttpMessageConverter<?>> messageConverters = restTemplate.getMessageConverters();
        converters.addAll(messageConverters);
        restTemplate.setMessageConverters(converters);
        return restTemplate;
    }


    /**
     * Get
     *
     * @param <T>           parameter
     * @param url           url
     * @param headers       headers
     * @param requestObject request object
     * @param responseType  response type
     * @return the t
     * @since 1.0.0
     */
    public <T> T get(String url, Map<String, String> headers, Object requestObject, Class<T> responseType) {
        return sendRequest(url, HttpMethod.GET, headers, requestObject, responseType);
    }

    /**
     * Get
     *
     * @param <T>           parameter
     * @param url           url
     * @param requestObject request object
     * @param responseType  response type
     * @return the t
     * @since 1.0.0
     */
    public <T> T get(String url, Object requestObject, Class<T> responseType) {
        return sendRequest(url, HttpMethod.GET, null, requestObject, responseType);
    }

    /**
     * Get
     *
     * @param <T>          parameter
     * @param url          url
     * @param responseType response type
     * @return the t
     * @since 1.0.0
     */
    public <T> T get(String url, Class<T> responseType) {
        return sendRequest(url, HttpMethod.GET, null, null, responseType);
    }

    /**
     * Post
     *
     * @param <T>           parameter
     * @param url           url
     * @param headers       headers
     * @param requestObject request object
     * @param responseType  response type
     * @return t t
     * @since 1.0.0
     */
    public <T> T post(String url, Map<String, String> headers, Object requestObject, Class<T> responseType) {
        return sendRequest(url, HttpMethod.POST, headers, requestObject, responseType);
    }

    /**
     * Post
     *
     * @param <T>           parameter
     * @param url           url
     * @param requestObject request object
     * @param responseType  response type
     * @return the t
     * @since 1.0.0
     */
    public <T> T post(String url, Object requestObject, Class<T> responseType) {
        return sendRequest(url, HttpMethod.POST, null, requestObject, responseType);
    }

    /**
     * Post
     *
     * @param <T>          parameter
     * @param url          url
     * @param responseType response type
     * @return the t
     * @since 1.0.0
     */
    public <T> T post(String url, Class<T> responseType) {
        return sendRequest(url, HttpMethod.POST, null, null, responseType);
    }

    /**
     * Send request
     *
     * @param <T>           parameter
     * @param url           url
     * @param method        method
     * @param headers       headers
     * @param requestObject request object
     * @param responseType  response type
     * @return the t
     * @since 1.0.0
     */
    public <T> T sendRequest(String url, HttpMethod method,
                             Map<String, String> headers,
                             Object requestObject,
                             Class<T> responseType) {
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        if (headers != null) {
            headers.forEach(httpHeaders::add);
        }
        HttpEntity<Object> httpEntity = new HttpEntity<>(requestObject, httpHeaders);
        ResponseEntity<T> responseEntity = this.restTemplate.exchange(url, method, httpEntity, responseType);
        return responseEntity.getBody();
    }

}
相关推荐
xmh-sxh-13143 分钟前
jdk各个版本介绍
java
天天扭码23 分钟前
五天SpringCloud计划——DAY2之单体架构和微服务架构的选择和转换原则
java·spring cloud·微服务·架构
程序猿进阶23 分钟前
堆外内存泄露排查经历
java·jvm·后端·面试·性能优化·oom·内存泄露
FIN技术铺28 分钟前
Spring Boot框架Starter组件整理
java·spring boot·后端
小曲程序35 分钟前
vue3 封装request请求
java·前端·typescript·vue
陈王卜1 小时前
django+boostrap实现发布博客权限控制
java·前端·django
小码的头发丝、1 小时前
Spring Boot 注解
java·spring boot
java亮小白19971 小时前
Spring循环依赖如何解决的?
java·后端·spring
飞滕人生TYF1 小时前
java Queue 详解
java·队列
武子康1 小时前
大数据-230 离线数仓 - ODS层的构建 Hive处理 UDF 与 SerDe 处理 与 当前总结
java·大数据·数据仓库·hive·hadoop·sql·hdfs