HTTP调用--- RestTemplate

1. get方法


1)String response=restTemplate.getForObject(builder.build().toString(),String.class);

2)Object response2 = restTemplate.exchange(url.toString(), HttpMethod.GET, request233, Object.class);

3)Object response3 = restTemplate.exchange(builder.build().toString(), HttpMethod.GET, request233, Object.class);

        String urlFinal = reqUrl + paramsUrl.substring(0, paramsUrl.length()-1).replace("'","\"");
        log.info("本次调用URL:" + urlFinal + ",入参为:" + JSON.toJSON(params));

        HttpEntity<MultiValueMap<String, Object>> request233 = new HttpEntity(null, headers);

        String response=restTemplate.getForObject(builder.build().toString(),String.class);
        log.info("本次调用返回值response:" + JSON.toJSON(response));

        URL url22 = new URL(urlFinal);
        Object response2 = restTemplate.exchange(url22.toString(), HttpMethod.GET, request233, Object.class);
        log.info("本次调用返回值response2:" + JSON.toJSON(response2));

        log.info("本次调用URL:" + builder.build().toString() + ",入参为:" + JSON.toJSON(params));
        Object response3 = restTemplate.exchange(builder.build().toString(), HttpMethod.GET, request233, Object.class);
        log.info("本次调用返回值response3:" + JSON.toJSON(response3)); 

但是已上方法存在一个不能解决的问题:

解决RestTemplate 请求url中包含百分号 会被转义成25的问题_Java_软件编程 - 编程客栈

所以还是建议用一下方法:

处理类似:http://XXXXX.com/api//cards?pw=???&u=??&aa="存在汉字的字符串"

1)URLEncoder.encode():用于URL中存在汉字时转义

  2)ResponseEntity<Map> response=restTemplate.getForEntity(uri, Map.class);

public String httpRequestGetWithMultiParams(String host, String url, String request) {

        String res = null;

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

        String reqUrl = host + url;

        Map<String, Object> params = JSONObject.parseObject(request, Map.class);
        String paramsUrl = "?";
        Object[] keys = params.keySet().toArray();

        for (int i = 0; i < params.size() ; i++) {
            String key = keys[i].toString();
            String value = params.get(key).toString();
            if(! "".equals(value)){
                if(isContainChinese(value)){
                    paramsUrl = paramsUrl + key + "=" + URLEncoder.encode(value.replace("'","\"")) + "&";
                }else {
                    paramsUrl = paramsUrl + key + "=" + value + "&";
                }
            }
        }
        String urlFinal = reqUrl + paramsUrl.substring(0, paramsUrl.length()-1);

        URI uri = null;
        try {
            uri = new URI(urlFinal);
        } catch (URISyntaxException e) {
            log.info("本次调用URISyntaxException:"  + e);
        }
        log.info("本次调用URL:" + uri + ",入参为:" + JSON.toJSON(params));
        ResponseEntity<Map> response=restTemplate.getForEntity(uri, Map.class);
        log.info("本次调用返回值:" + response.getBody());

        log.info("本次调用返回值:" + JSON.toJSON(response.getBody()));
        return JSON.toJSON(response.getBody()).toString();
    }

2.post方法

ResponseEntity<JSONObject> response = restTemplate.exchange(reqUrl, HttpMethod.POST, entity, JSONObject.class);
        String res = null;

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

        String reqUrl = host + url;

        // HttpEntity<String> entity = new HttpEntity<String>(request.toString(),null);
        HttpEntity<JSONObject> entity = new HttpEntity<JSONObject>(request, null);

        log.info("本次调用URL:" + reqUrl + ",入参为:" + JSON.toJSON(entity));
        ResponseEntity<JSONObject> response = restTemplate.exchange(reqUrl, HttpMethod.POST, entity, JSONObject.class);
        log.info("本次调用返回值:" + JSON.toJSON(response.getBody()));
        return response.getBody().toString();

2.1 post方法

RestTemplate乱码和No HttpMessageConverter for java.util.HashMap and content type “application/json - 愤怒的苹果ext - 博客园

处理url带参数的post请求:http://XXXXX.com/api//cards?pw=???&u=??&aa="存在汉字的字符串"

        StringHttpMessageConverter m = new StringHttpMessageConverter(Charset.forName("UTF-8"));
        RestTemplate restTemplate = new RestTemplateBuilder().additionalMessageConverters(m).additionalMessageConverters(new MappingJackson2HttpMessageConverter()).build();

        String res = null;

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        String reqUrl = host + url;

        Map<String, Object> params = JSONObject.parseObject(request, Map.class);
        String paramsUrl = "?";
        Object[] keys = params.keySet().toArray();

        for (int i = 0; i < params.size() ; i++) {
            String key = keys[i].toString();
            String value = params.get(key).toString();
            if (! "".equals(value)){
                paramsUrl = paramsUrl + key + "=" + value + "&";
            }
        }
        String urlFinal = reqUrl + paramsUrl.substring(0, paramsUrl.length() - 1);

        URI uri = null;
        try {
            uri = new URI(urlFinal);
        } catch (URISyntaxException e) {
            log.info("本次调用URISyntaxException:"  + e);
        }
        log.info("本次调用URL:" + uri + ",入参为:" + JSON.toJSON(params));

        ResponseEntity<String> response= restTemplate.postForEntity(uri, params, String.class);

        log.info("本次调用返回值:" + JSON.toJSON(response.getBody()));
        return JSON.toJSON(response.getBody()).toString();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值