Springmvc的RestTemplate

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import org.junit.Test;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.xml.MarshallingHttpMessageConverter;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import com.finance.util.StringUtil;
import com.framework.Config;

@Component
public class RestClient {

    private final static Logger log = Logger.getLogger(RestClient.class);

    private static RestTemplate restTemplate;

    static {
        createRestTemplate();
    }

    public static RestTemplate getRestTemplate() {

        return restTemplate;

    }

    private static void createRestTemplate() {

        restTemplate = new RestTemplate();
        List<HttpMessageConverter> converters = new ArrayList>();// 消息体转换器列表
        MarshallingHttpMessageConverter marshalConverter = new MarshallingHttpMessageConverter();// xom类型的消息体转换器
        Jaxb2Marshaller marshaller = new Jaxb2Marshaller();// 创建JAXB2类型的xom环境
        marshalConverter.setMarshaller(marshaller);// 设置编组器
        marshalConverter.setUnmarshaller(marshaller);// 设置解组器
        converters.add(marshalConverter);// 将xom消息体转换器添加到列表
        converters.add(new StringHttpMessageConverter());
        restTemplate.setMessageConverters(converters);// 将转换器列表放入RestTemplate

    }

    
    public static String doPost(Map<String, Object> param, String intername) {

        String path = Config.getValue(intername);

        if (StringUtil.isEmpty(path)) {
            log.error("接口地址为空,intername==>" + intername);
            return null;
        }

        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());

        JSONObject jsonObj = JSONObject.fromObject(param);
        HttpEntity formEntity = new HttpEntity(jsonObj.toString(), headers);

        return getRestTemplate().postForObject(path, formEntity, String.class);

    }
    
    
    public static String doPost(String param, String intername) {

        String path = Config.getValue(intername);

        if (StringUtil.isEmpty(path)) {
            log.error("接口地址为空,intername==>" + intername);
            return null;
        }

        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());

        HttpEntity formEntity = new HttpEntity(param, headers);

        return getRestTemplate().postForObject(path, formEntity, String.class);

    }
    
    
    public static String doPost_HTTPS(NameValuePair[] param, String intername) {

        String result = "";
        String path = Config.configMap.get(intername);

        if (StringUtil.isEmpty(path)) {
            log.error("接口地址为空,intername==>" + intername);
            return null;
        }
        try {
            HttpClient httpCLient = new SSLClient();
            HttpPost httpPost = new HttpPost(path);
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(Arrays.asList(param), "UTF-8");
            entity.setContentType("application/x-www-form-urlencoded; charset=UTF-8");
            httpPost.setEntity(entity);
            HttpResponse response = httpCLient.execute(httpPost);
            if (entity != null) {
                result = EntityUtils.toString(response.getEntity(), "UTF-8");
            }
            EntityUtils.consume(entity);

        } catch (Exception e) {
            e.printStackTrace();

        } finally {
        }
        return result;
    }
    
    public static void main(String[] args) {
        Map<String, Object> map = new HashMap<String, Object>();
        String content = RestClient.doPost(map, "");
        System.out.println("content = "+content);
        
    }
}

评论

暂无评论