RestClient使用

前言

RestClient是Spring6.1使用的http客户端,旨在用来代替RestTemplate发送http,以阻塞方式发送和接收 HTTP 请求和响应,随着Springboot3.2.0的发布,RestClient才可以被使用

RestClient使用

get请求发送

定义一个Get请求的test接口

typescript 复制代码
  @GetMapping("/test")
    public String test() {
        log.info("info日志");
        return "hello";
    }

使用RestClient客户端发送get请求,具体如下

typescript 复制代码
    @RequestMapping("/hello")
    public String hello() {
        RestClient restClient = RestClient.create();
        String result = restClient.get().uri("http://localhost:7028/test").retrieve().body(String.class);
        log.info("返回结果为:{{}}", result);
        return "success";
    }

post请求

定义一个post和json格式的请求

less 复制代码
    @PostMapping("/save")
    public String save(@RequestBody User user) {
        log.info("数据为:{{}}", user);
        return "hello";
    }

使用RestClient客户端发送post请求,具体如下

ini 复制代码
@RequestMapping("/testSave")
    public String testSave() throws JsonProcessingException {
        JsonMapper jsonMapper = JsonMapper.builder()
                .configure(JsonReadFeature.ALLOW_SINGLE_QUOTES, true)
                .build();
        RestClient restClient = RestClient.create();
        User user = new User();
        user.setId(1L);
        user.setName("aaa");
        String json = jsonMapper.writeValueAsString(user);
        String result = restClient.post()
                .uri("http://localhost:7028/save")
                .contentType(APPLICATION_JSON)
                .body(json).retrieve().body(String.class);
        log.info("返回结果为:{{}}", result);
        return "success";
    }

delete请求

定义一个delete接口

less 复制代码
    @DeleteMapping("/deleteById/{id}")
    public String deleteById(@PathVariable("id") Long id) {
        log.info("删除id为:{{}}", id);
        return "success";
    }

使用RestClient客户端发送delete请求,具体如下

typescript 复制代码
   @RequestMapping("/testDeleteById")
    public String testDeleteById() {
        RestClient restClient = RestClient.create();
        String result = restClient.delete().uri("http://localhost:7028/deleteById/{id}", 1)
                .retrieve().body(String.class);
        log.info("返回结果为:{{}}", result);
        return "success";
    }

put请求

定义一个put请求

less 复制代码
 @PutMapping("/update")
    public String update(@RequestBody User user) {
        log.info("数据为:{{}}", user);
        return "hello";
    }

使用RestClient客户端发送put请求,具体如下

ini 复制代码
@RequestMapping("/testUpdate")
    public String testUpdate() throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        RestClient restClient = RestClient.create();
        User user = new User();
        user.setId(1L);
        user.setName("aaa");
        String json = objectMapper.writeValueAsString(user);
        String result = restClient.put()
                .uri("http://localhost:7028/update")
                .contentType(APPLICATION_JSON)
                .body(json).retrieve().body(String.class);
        log.info("返回结果为:{{}}", result);
        return "success";
    }

总结

RestClient是spring6.1版本以上才有的,但是好用的http工具类很多,可以根据自己习惯做技术选型,提高自己的开发效率

相关推荐
小蒜学长几秒前
基于Python的微博热搜话题可视化分析系统的设计与实现(代码+数据库+LW)
后端·python·django·数据可视化·情感分析·微博热搜
BingoGo3 分钟前
Whim 是什么?一门把 PHP 被否决的设想真跑起来的实验语言
后端·php
ss2735 分钟前
Java全栈实战 | 1.3-03 索引原理:联合索引 (a,b,c) 只查 b 走不了索引?B+ 树一图讲透所有索引玄学
java·数据库
前端初见15 分钟前
Java+AI零基础入门到大牛
java·spring boot·spring·java-ee·intellij-idea
猫猫不是喵喵.18 分钟前
两个不同应用Session能互相访问解决
java
IT_陈寒22 分钟前
Java序列化坑了我三天,原来问题出在这个默认方法
前端·人工智能·后端
带多刺的玫瑰23 分钟前
Leecode#35刷题之搜索插入位置
java·python·算法
一条泥憨鱼25 分钟前
苍穹外卖【day10| 来电提醒和客户催单】
java·后端·websocket·苍穹外卖
SunnyDays101126 分钟前
Java 设置 Excel 数据验证:整数、日期、文本长度、下拉列表和时间
java·excel·数据验证·下拉列表
编程_大白33 分钟前
@SpringBootTest
java