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工具类很多,可以根据自己习惯做技术选型,提高自己的开发效率

相关推荐
Java牛马1 小时前
SpringBoot Starter 依赖相关总结
java·spring·springboot·starter·自动装配·依赖
吴声子夜歌1 小时前
Java面试——Spring Cloud原理及应用(二)
java·spring cloud·面试
SomeB1oody2 小时前
【RustyML入门】6.3. 并行归约
开发语言·后端·机器学习·rust·教程
东小西2 小时前
【SAA实战】第 1 篇:ReactAgent 入门——先撸个"会调工具的助手"跑起来
java·人工智能·spring
Profile排查笔记2 小时前
指纹浏览器哪个好?从 Profile、代理、权限和自动化能力判断是否适合
前端·人工智能·后端·自动化
用户938515635073 小时前
用 AI 结对编程从 0 搭一个"单词后台管理系统":Next.js + Supabase + Drizzle + shadcn/ui 全记录
后端·postgresql·next.js
mqiqe3 小时前
AgentScope Java 2.0 协议集成全景解析:A2A、AG-UI、Agent Protocol 三大开放协议实战指南
java·开发语言·ui
脉动数据行情3 小时前
Java 实现台股 TWSE/TPEx 行情采集(个股 + K 线)
java·开发语言·twse·tpex·台股
爱学习的小邓同学3 小时前
Golang --- (1)第一个Golang程序
开发语言·后端·golang
小灰灰搞电子4 小时前
Rust+Slint 实现的“DNA双螺旋”加载动画源码分享
后端·rust·slint·加载动画