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<HttpMessageConverter<?>>();// 消息体转换器列表
        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<String> formEntity = new HttpEntity<String>(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<String> formEntity = new HttpEntity<String>(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);
        
    }
}

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )