后端通过RestTemplate发送Get、Post请求第三方API以及模拟请求响应

1 Pom文件中导入所需依赖,以boot项目为例
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.33</version>
</dependency>

RestTemplate依赖在starter-web内,fastjson依赖Post方法会用到;

2 后端发起Get请求Demo
@Service
public class RestTemp {

    @Autowired
    private RestTemplate restTemplate;

     public void doGet(){
        String url = "http://localhost:8080/testGet/{name}?id={id}";    //模拟第三方API
        HashMap<String, Object> map = new HashMap<>();
        map.put("name","张三");
        map.put("id","666");
        ResponseEntity<String> forEntity = restTemplate.getForEntity(url, String.class, map);
        System.out.println(forEntity.getHeaders()); //  [content-type xxxxx]
        System.out.println(forEntity.getBody());    // "xxxx"
        System.out.println(forEntity.getStatusCode());  // 200 OK
        System.out.println(forEntity.getStatusCodeValue()); // 200
    }
}
3 后端接收Get请求的Controller Demo
@RequestMapping
@RestController
public class TestController {

    @GetMapping("/testGet/{name}")
    public String getMethod(@PathVariable("name")String name,@RequestParam("id") Integer id){
        System.out.println("getMethod接口访问成功"+id+"----"+name);
        return "成功了~";
    }

}

这里注意@PathVariable和@RequestParam两个注解的使用区别;

4 后端发起Post请求Demo
@Service
public class RestTemp {

    @Autowired
    private RestTemplate restTemplate;
    /**
    以返回值为一个User对象为例,该对象有name和age属性,自己创建User实体类
    */
    public User doPost() throws URISyntaxException {
        URI uri = new URI("http://localhost:8080/testPost");//模拟第三方API
        HttpHeaders headers = new HttpHeaders();
        headers.set("Accept-Charset", "UTF-8");
        headers.set("Content-Type", "application/json; charset=utf-8");//设置请求头为json格式
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();//添加参数,HttpEntity里面的参数是MultiValueMap类型
        map.add("name","richlu");
        map.add("id", "666");
        HttpEntity<Object> requestEntity = new HttpEntity<>(map, headers);
        String s = restTemplate.postForObject(uri, requestEntity, String.class);
        JSONObject jsonObject = JSON.parseObject(s);
        String name = jsonObject.getString("name");
        Integer age = (Integer)jsonObject.get("age");
        User user = new User(name, age);
        return user;
    }

}
4 后端接受Post请求的Controller Demo
@RequestMapping
@RestController
public class TestController {

    @PostMapping ("/testPost")
    public User postMethod(@RequestBody Object obj){
        System.out.println("postMethod接口访问成功");
        System.out.println(obj);
        User user = new User();
        user.setName("张三");
        user.setAge(13);
        return user;
    }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Richlu.java

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值