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

相关推荐
蓝斯4975 小时前
[原创]《C#高级GDI+实战:从零开发一个流程图》第章:增加贝塞尔曲线,上、下、左、右连接点
java·c#·流程图
前端工作日常5 小时前
我学习到的Java 的 Service 分层:itf 和 impl 到底是什么?
java·后端
都叫我大帅哥6 小时前
Spring @Transactional 注解完全指南
java·spring
Java成神之路-6 小时前
G1 垃圾回收器 :SATB + TAMS核心机制深度解析
java·jvm
前端工作日常6 小时前
我学习到的JIT即时编译与机器码缓存失效
java·后端
带刺的坐椅6 小时前
Solon 的 10 种 HTTP 服务器:改一行依赖,换一个引擎
java·solon·jetty·undertow·mcp-server·htttp
犀利豆6 小时前
Claude Code Tools 研究系列(三)ExitPlanMode
人工智能·后端
星栈7 小时前
oh-my-pi工程级AI编码工使用体验
人工智能·后端·agent
xcLeigh7 小时前
Go入门:变量声明的五种方式详解
java·开发语言·golang
AI大模型-小华7 小时前
Codex 三方充值快速入门指南
java·前端·数据库·chatgpt·ai编程·codex·chatgpt pro