springboot中的restTemplate访问get,post请求的各种方式

springboot中封装好了访问外部请求的方法类,那就是RestTemplate。下面就简单介绍一下,RestTemplate访问外部请求的方法。

get请求

首先get请求的参数是拼接在url后面的。所以不需要额外添加参数。但是也需要分两种情况。

1、 有请求头

由于 getForEntity()getForObject() 都无法加入请求头。所以需要请求头的连接只能使用 exchange() 来访问。代码如下

public JSONObject test(){
        try {
            RestTemplate re = new RestTemplate();
            HttpHeaders headers = new HttpHeaders();
            String url = "http://test.api.com?id=123";
            headers.set("Content-Type","application/json");
            HttpEntity<JSONObject> jsonObject= re.exchange(url, HttpMethod.GET,new HttpEntity<>(headers),JSONObject.class);
            log.info("返回:{}",jsonObject.getBody());
            return jsonObject.getBody();
        }catch (Exception e){
            log.error(e.getMessage());
        }
        return null;
    }
2、 无请求头

无需请求头的可以用三个方法实现。getForEntity()getForObject() 还有 exchange() 都可以实现。下面讲前两种用的比较多的。
1、getForEntity()

public JSONObject test(){

        try {
            RestTemplate re = new RestTemplate();
            String url = "http://api.help.bj.cn/apis/alarm/?id=101020100";
            HttpEntity<JSONObject> jsonObject= re.getForEntity(url,JSONObject.class);
            log.info("返回:{}",jsonObject.getBody());
            return jsonObject.getBody();
        }catch (Exception e){
            log.error(e.getMessage());
        }
        return null;
    }

1、getForObject()

public JSONObject test(){

        try {
            RestTemplate re = new RestTemplate();
            String url = "http://api.help.bj.cn/apis/alarm/?id=101020100";
            JSONObject jsonObject= re.getForObject(url,JSONObject.class);
            log.info("返回:{}",jsonObject);
            return jsonObject;
        }catch (Exception e){
            log.error(e.getMessage());
        }
        return null;
    }

post请求

post请求也分几种情况

1、 参数在body的form-data里面
public static JSONObject test(){

        try {
            RestTemplate re = new RestTemplate();
            String url = "http://localhost:8101/test";
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.MULTIPART_FORM_DATA);

            MultiValueMap<String, Object> loginJson = new LinkedMultiValueMap<>();
            loginJson.add("id", "123");

            JSONObject jsonObject= re.postForObject(url,new HttpEntity<>(loginJson,headers),JSONObject.class);
            log.info("返回:{}",jsonObject);
            return jsonObject;
        }catch (Exception e){
            log.error(e.getMessage());
        }
        return null;
    }

还可以把 postForObject 换成 postForEntity

1、 参数在body的x-www-from-urlencodeed里面

只需要把请求头的setContentType改成下面即可headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

public static JSONObject test(){

        try {
            RestTemplate re = new RestTemplate();
            String url = "http://localhost:8101/test";
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

            MultiValueMap<String, Object> loginJson = new LinkedMultiValueMap<>();
            loginJson.add("id", "123");

            JSONObject jsonObject= re.postForObject(url,new HttpEntity<>(loginJson,headers),JSONObject.class);
            log.info("返回:{}",jsonObject);
            return jsonObject;
        }catch (Exception e){
            log.error(e.getMessage());
        }
        return null;
    }
1、 参数在body的raw里面

在这里插入图片描述

 public static JSONObject test(){

        try {
            RestTemplate re = new RestTemplate();
            String url = "http://localhost:8101/test";
            HttpHeaders headers = new HttpHeaders();
            headers.set("Content-Type","application/json");
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id","1");

            JSONObject jsonObject1 = restTemplate
                    .postForObject(url,new HttpEntity<>(jsonObject,headers),JSONObject.class);

            log.info("返回:{}",jsonObject1);
            return jsonObject;
        }catch (Exception e){
            log.error(e.getMessage());
        }
        return null;
    }
  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值