远程接口调用中 spring的RestTemplate模板类使用

远程接口调用中 spring的RestTemplate模板类使用

@Test
    public void testGet(){
        String path="http://api.tianapi.com/txapi/saylove/index?key=db3d22eebbff236d1b3b424ea12c3dd6";
        RestTemplate t = new RestTemplate();
        String result = t.getForObject(path, String.class);
        System.out.println(result);
    }

    @Test
    public void testRestUrlGet(){
        String path="http://localhost:8080/user/{0}";
        RestTemplate t = new RestTemplate();
        String result = t.getForObject(path, String.class,22);
        System.out.println(result);
    }

    @Test
    public void testRestUrlGet2(){
        String path="http://localhost:8080/user/{uid}";
        RestTemplate t = new RestTemplate();
        Map<String,Object> map = new HashMap<>();
        map.put("uid",22);
        String result = t.getForObject(path, String.class,map);
        System.out.println(result);
    }

    @Test
    public void testPost(){
        String path = "http://localhost:8080/user/{0}";
        RestTemplate t = new RestTemplate();
        Sysuser u = new Sysuser();
        u.setUname("mnb");
        u.setUpwd("mmmm");
        u.setUphone("11111");
        String s = t.postForObject(path, u, String.class);//默认的请求头Content-type:application/json
        System.out.println(s);
    }

    @Test
    public void testPut(){
        String path = "http://localhost:8080/user";
        RestTemplate t = new RestTemplate();
        Sysuser u = new Sysuser();
        u.setUid(23);
        u.setUname("mnb2");
        u.setUpwd("mmmm2");
        u.setUphone("22222");
       t.put(path,u);//可以向请求体放数据,没有返回值
    }

    @Test
    public void testDelete(){
        String path = "http://localhost:8080/user/{0}";
        RestTemplate t = new RestTemplate();//无法向请求体放数据,没有返回值
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值