当前位置: 首页 > news >正文

spring boot3.0新特性Http客户端远程调用

1、安装依赖

<!-- For reactive support --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-webflux</artifactId></dependency>

2、项目结构
在这里插入图片描述
3、新建配置类WebConfig

package com.example.springboot3.config;import com.example.springboot3.client.UserClient;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.support.WebClientAdapter;
import org.springframework.web.service.invoker.HttpServiceProxyFactory;@Configuration
public class WebConfig {
//    @Bean
//    WebClient webClient(ObjectMapper objectMapper) {
//        return WebClient.builder()
//                .baseUrl("http://localhost:8001")
//                .build();
//    }@BeanWebClient webClient() {return WebClient.builder().baseUrl("http://localhost:8001").build();}@SneakyThrows@BeanUserClient postClient(WebClient webClient) {HttpServiceProxyFactory httpServiceProxyFactory =HttpServiceProxyFactory.builder(WebClientAdapter.forClient(webClient)).build();return httpServiceProxyFactory.createClient(UserClient.class);}
}

4、新建Http客户接口UserClient

package com.example.springboot3.client;import org.springframework.web.service.annotation.GetExchange;
import org.springframework.web.service.annotation.HttpExchange;@HttpExchange(accept = "application/json", contentType = "application/json")
public interface UserClient {@GetExchange("/dubboTest")String getAllTest();
}

5、controller测试接口

package com.example.springboot3.controller;import com.example.springboot3.client.UserClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class TestController {@AutowiredUserClient userClient;@GetMapping("/test")public String test(){return userClient.getAllTest();}
}

6、调用的方式
@HttpExchange:是指定HTTP端点的通用注释。当在接口级别使用时,它适用于所有方法。
@GetExchange:为HTTP GET请求指定@HttpExchange。
@PostExchange:对于HTTP POST请求,指定@HttpExchange。
@PutExchange:为HTTP PUT请求指定@HttpExchange。
@DeleteExchange:对于HTTP DELETE请求,指定@HttpExchange。
@PatchExchange:对于HTTP Patch请求,指定@HttpExchange。
7、传递参数格式
@PathVariable:将请求URL中的值替换为占位符。
@RequestBody:提供请求的主体。
@RequestParam:添加请求参数。当“content-type”设置为“application/x-www-form-urlencoded”时,请求参数会在请求体中编码。否则,它们将作为URL查询参数添加。
@ requesttheader:添加请求头的名称和值。
@RequestPart:可用于添加请求部分(表单字段,资源或HttpEntity等)。
@CookieValue:向请求中添加cookie。

http://www.lryc.cn/news/58745.html

相关文章:

  • 查询联系:多表查询 - 1
  • 「Bug」OpenCV读取图像为 None 分析
  • EVO——视觉里程计/SLAM轨迹评估工具
  • TCP为什么要三次握手,而不是两次或四次?
  • git 命令:工作日常使用
  • Http和Https
  • 【计算机网络复习】第三章 传输层 2
  • 你真的会自动化测试?自动化测试技术选型抉择
  • 【id:31】【20分】A. Point(类与构造)
  • ASM字节码处理工具原理及实践(二)
  • Golang每日一练(leetDay0030)
  • QT5.15.2 在线安装下载速度慢的解决办法
  • Cadence Allegro 导出Waived Design Rules Check Report报告详解
  • Java阶段一Day19
  • radmin远程控制软件怎么样,有没有替代品
  • Java反射面试总结(一)
  • 【论文阅读】3D-LaneNet
  • Kafka的概念|架构|搭建|查看命令
  • 大数据项目实战之数据仓库:电商数据仓库系统——第5章 数据仓库设计
  • OpenHarmony社区运营报告(2023年3月)
  • 杰林码图像增强算法——超分辨率、图像放大、轮廓和色彩强化算法(二)
  • 在three.js中废置对象
  • Java中的String类真的不可变吗?
  • 电脑重装了系统开不了机怎么办?
  • SPOJ-NSUBSTR - Substrings(SAM求所有长度子串的最大出现次数)
  • Mariadb10.5基于同服务器多实例主从配置
  • linux 修改主机名称
  • 学校的地下网站(学校的地下网站1080P高清)
  • 勒索病毒是什么?如何防勒索病毒
  • SpringBoot+VUE+Axios 【链接超时】 后端正常返回结果,前端却出现错误无法接收数据