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

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



<dependency>
	<groupId>com.fasterxml.jackson.coregroupId>
	<artifactId>jackson-coreartifactId>
dependency>
<dependency>
	<groupId>com.fasterxml.jackson.coregroupId>
	<artifactId>jackson-databindartifactId>
dependency>
<dependency>
	<groupId>com.fasterxml.jackson.coregroupId>
	<artifactId>jackson-annotationsartifactId>
dependency>
/**
 * 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            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            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           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            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            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           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            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();
    }

}

你可能感兴趣的:(Java,http,java)