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

}
相关推荐
hanbarger几秒前
mybatis框架——缓存,分页
java·spring·mybatis
cdut_suye8 分钟前
Linux工具使用指南:从apt管理、gcc编译到makefile构建与gdb调试
java·linux·运维·服务器·c++·人工智能·python
苹果醋320 分钟前
2020重新出发,MySql基础,MySql表数据操作
java·运维·spring boot·mysql·nginx
小蜗牛慢慢爬行21 分钟前
如何在 Spring Boot 微服务中设置和管理多个数据库
java·数据库·spring boot·后端·微服务·架构·hibernate
azhou的代码园24 分钟前
基于JAVA+SpringBoot+Vue的制造装备物联及生产管理ERP系统
java·spring boot·制造
wm10431 小时前
java web springboot
java·spring boot·后端
smile-yan1 小时前
Provides transitive vulnerable dependency maven 提示依赖存在漏洞问题的解决方法
java·maven
老马啸西风1 小时前
NLP 中文拼写检测纠正论文-01-介绍了SIGHAN 2015 包括任务描述,数据准备, 绩效指标和评估结果
java
Earnest~1 小时前
Maven极简安装&配置-241223
java·maven
皮蛋很白1 小时前
Maven 环境变量 MAVEN_HOME 和 M2_HOME 区别以及 IDEA 修改 Maven repository 路径全局
java·maven·intellij-idea