HttpRestClient.java 5.58 KB
package com.ui.http;

import com.ui.config.SystemConfig;
import com.ui.model.BaseResponse;
import com.ui.model.req.User;
import org.apache.http.client.methods.HttpGet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Map;

public class HttpRestClient {

    private static final Logger logger = LoggerFactory
            .getLogger(HttpRestClient.class);

    private RestTemplate restTemplate;

    private SystemConfig systemConfig;

    public <T> T defaultPost(String uri, Object request, Class<T> responseType) {
        try {
            return restTemplate.postForObject(systemConfig.getServiceUrl() + uri, request,responseType);
        } catch (Exception e) {
            logger.error("postForObject failed!url:"+uri, e);
            return null;
        }
    }

    public <T> T defaultGet(String uri, Class<T> responseType,Map<String, ?> map) {
        try {
            StringBuilder url = new StringBuilder(uri);
            if(map != null){
                url.append("?");
                for (Map.Entry<String, ?> entry : map.entrySet()) {
                    url.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
                }
                return restTemplate.getForObject(systemConfig.getServiceUrl() + url.toString().substring(0, url.length()-1), responseType);
            }
            return restTemplate.getForObject(systemConfig.getServiceUrl() + uri, responseType);
        } catch (Exception e) {
            logger.error("getForObject failed!url: "+uri, e);
            return null;
        }
    }

    public  <T> T  exchangeForget(String uri, ParameterizedTypeReference<T> responseType,Map<String, ?> map) {
        try {
            StringBuilder url = new StringBuilder(uri);
            if(map != null){
                url.append("?");
                for (Map.Entry<String, ?> entry : map.entrySet()) {
                    url.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
                }
                HttpEntity<T> response =  restTemplate.exchange(systemConfig.getServiceUrl() + url.toString().substring(0, url.toString().length()-1), HttpMethod.GET, null, responseType);
                return response.getBody();
            }
            return restTemplate.exchange(systemConfig.getServiceUrl() + uri, HttpMethod.GET, null, responseType).getBody();
        } catch (Exception e) {
            logger.error("getForObject failed!url: "+uri, e);
            return null;
        }
    }

    public  <T> T  exchangeForpost(String uri, ParameterizedTypeReference<T> responseType,Object requst) {
        try {
            HttpEntity httpEntity = new HttpEntity(requst);
            return restTemplate.exchange(systemConfig.getServiceUrl() + uri, HttpMethod.POST, httpEntity, responseType).getBody();
        } catch (Exception e) {
            logger.error("getForObject failed!url: "+uri, e);
            return null;
        }
    }


    public  <T> T  otherOxchangeForget(String uri, ParameterizedTypeReference<T> responseType,Map<String, ?> map) {
        try {
            StringBuilder url = new StringBuilder(uri);
            if(map != null){
                url.append("?");
                for (Map.Entry<String, ?> entry : map.entrySet()) {
                    url.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
                }
                HttpEntity<T> response =  restTemplate.exchange( url.toString().substring(0, url.toString().length()-1), HttpMethod.GET, null, responseType);
                return response.getBody();
            }
            return restTemplate.exchange( uri, HttpMethod.GET, null, responseType).getBody();
        } catch (Exception e) {
            logger.error("getForObject failed!url: "+uri, e);
            return null;
        }
    }

    public <T> T get(String url, Class<T> responseType,Map<String, ?> map) {
        try {
            StringBuilder uri = new StringBuilder(url);
            if(map != null){
                uri.append("?");
                for (Map.Entry<String, ?> entry : map.entrySet()) {
                    uri.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
                }
                return restTemplate.getForObject(uri.toString().substring(0, uri.length()-1), responseType);
            }
            return restTemplate.getForObject(url, responseType);
        } catch (Exception e) {
            logger.error("getForObject failed!url: "+url, e);
            return null;
        }
    }

    public <T> T post(String url, Object request, Class<T> responseType) {
        try {
            return restTemplate.postForObject(url, request,responseType);
        } catch (Exception e) {
            logger.error("postForObject failed!url:"+url, e);
            return null;
        }
    }

    public <T> T defaultGet(String uri, Class<T> responseType) {
        try {
            return restTemplate.getForObject(systemConfig.getServiceUrl() + uri, responseType);
        } catch (Exception e) {
            logger.error("getForObject failed!url: "+uri, e);
            return null;
        }
    }

    public void setRestTemplate(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }
    public void setSystemConfig(SystemConfig systemConfig) {
        this.systemConfig = systemConfig;
    }
}