封装一个简单的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();
    }

}
相关推荐
尚学教辅学习资料7 分钟前
基于SpringBoot的医药管理系统+LW示例参考
java·spring boot·后端·java毕业设计·医药管理
雷神乐乐23 分钟前
File.separator与File.separatorChar的区别
java·路径分隔符
小刘|27 分钟前
《Java 实现希尔排序:原理剖析与代码详解》
java·算法·排序算法
逊嘘1 小时前
【Java语言】抽象类与接口
java·开发语言·jvm
morris1311 小时前
【SpringBoot】Xss的常见攻击方式与防御手段
java·spring boot·xss·csp
‍。。。1 小时前
使用Rust实现http/https正向代理
http·https·rust
七星静香1 小时前
laravel chunkById 分块查询 使用时的问题
java·前端·laravel
Jacob程序员1 小时前
java导出word文件(手绘)
java·开发语言·word
ZHOUPUYU1 小时前
IntelliJ IDEA超详细下载安装教程(附安装包)
java·ide·intellij-idea
stewie61 小时前
在IDEA中使用Git
java·git