openFegin传参 学习

  • 项目之间调用Feign
    • 简单依赖调用
    • openFegin 传参
      • Get请求
        • 单参数
        • 多参数
        • 对象传参
        • get传参代码
      • post 传参
        • 单参
        • 多参数
        • 对象
        • post 代码

项目之间调用Feign

简单依赖调用

  1. 依赖
 <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency>


2. 新建feign包 并创建相应服务调用接口 使用 @FeignClient(“注册服务name”)
3. 开启feign ** @EnableFeignClients** 或具体到扫描包**@EnableFeignClients(basePackages = {“comxx.xx.feign”})**

openFegin 传参

Get请求

单参数

单参数:必须加注解**@RequestParam** 否则请求不通过,消费者默认变为post请求 可能status 405 :

//消费报错
feign.FeignException$MethodNotAllowed: status 405 reading FirstFeignService#firstParam(String)//提供者警告
Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported]

多参数

get 多参数 必须加注解 @RequestParam 否则启动报错

//错误
String secondParam(  String uname,   Integer age);
//报错信息
Caused by: java.lang.IllegalStateException: Method has too many Body parameters
//正确写法
String secondParam(  @RequestParam String uname,   @RequestParam  Integer age);

对象传参

get 对象传参 @SpringQueryMap
以往get对象无法传参转Map 最新官网提供 @SpringQueryMap

官方文档
OpenFeign@QueryMap批注支持将POJO用作GET参数映射。不幸的是,默认的OpenFeign QueryMap注释与Spring不兼容,因为它缺少value属性。
Spring Cloud OpenFeign提供了等效的@SpringQueryMap注释用于注释POJO或Map参数作为查询参数映射。

get传参代码

1.消费者调用

//消费者调用/**无参*/@GetMapping(value = "frist/uuid")String  getUUid();/**get 单参*/@GetMapping(value = "frist/firstParamo")String firstParam( @RequestParam  String uname);/**get 多参*/@GetMapping(value = "frist/secondParam")String secondParam( @RequestParam  String uname,   @RequestParam  Integer age);/**get 对象*/@GetMapping(value = "frist/test/pojo")String testGetPojo(@SpringQueryMap  TestVo testVo);
  1. 提供者
     @RequestMapping("/uuid")public String fristTest(){return UUID.randomUUID().toString();}/**get 单参*/@GetMapping(value = "/firstParamo")String firstParam( String uname){System.out.println("firstParamo:"+uname);return "firstParamo:"+uname;}/**get 多参*/@GetMapping(value = "/secondParam")String secondParam(  String uname,  Integer age){System.out.println("secondParam:"+uname+":"+age);return "secondParam:"+uname+":"+age;}/**get 对象*/@GetMapping("/test/pojo")public  String testGetPojo(TestVo testVo){System.out.println(testVo);if(null!=testVo ||StringUtils.isNotBlank(testVo.getUname())||null!=testVo.getAge()){return  "数据不为空";}else {return "数据为空";}}

post 传参

使用@RequestBody 默认将请求转为post

单参

注意: 不加注解启动调用都不会报错,但是传参结果为null

  1. 使用 @RequestParam 提供者无需加
  2. 使用 @RequestBody 提供者必须加 @RequestBody 否则则无法获取结果为null

多参数

1.所有参数都加 @RequestParam
2.混合使用 @RequestBody @RequestParam
最多只能有一个参数是@RequestBody指明的,其余的参数必须使用@RequestParam指明
使用@RequestBody 必须用@RequestBody 接收

对象

  1. 使用 @SpringQueryMap
  2. 使用 @RequestBody

post 代码

  1. 消费者
     @PostMapping(value = "frist/postFirstParam")String postFirstParam(  @RequestBody  String uname);// String postFirstParam(  @RequestParam  String uname);@PostMapping(value = "frist/postSecondParam")String postSecondParam(@RequestParam  String uname, @RequestBody   Integer age);@PostMapping(value = "frist/postTestGetPojo")String postTestGetPojo(@RequestBody TestVo testVo);//String postTestGetPojo(@SpringQueryMapTestVo testVo);
  1. 提供者
  @PostMapping(value = "/postFirstParam")//RequestParam  // String postFirstParam( String uname){String postFirstParam( @RequestBody String uname){System.out.println("postFirstParam:"+uname);return "postFirstParam:"+uname;}@PostMapping(value = "/postSecondParam")String postSecondParam(String uname, @RequestBody Integer age){System.out.println("postSecondParam:"+uname+":"+age);return "postSecondParam:"+uname+":"+age;}@PostMapping(value = "/postTestGetPojo")//SpringQueryMap//  String postTestGetPojo(@RequestBody TestVo testVo){String postTestGetPojo(@RequestBody TestVo testVo){System.out.println("postTestGetPojo:"+testVo);return "postTestGetPojo";}

openFegin传参相关推荐

  1. Go 学习笔记(25)— 并发(04)[有缓冲/无缓冲通道、WaitGroup 协程同步、select 多路监听通道、close 关闭通道、channel 传参或作为结构体成员]

    1. 无缓冲的通道 无缓冲的通道(unbuffered channel)是指在接收前没有能力保存任何值的通道. 这种类型的通道要求发送 goroutine 和接收 goroutine 同时准备好,才能 ...

  2. MySQL 存储过程传参之in, out, inout 参数用法

    存储过程传参:存储过程的括号里,可以声明参数. 语法是 create procedure p([in/out/inout] 参数名  参数类型 ..) in :给参数传入值,定义的参数就得到了值 ou ...

  3. shell脚本的命令行传参

    在Linux环境下开发C程序,若想要可选择性的给程序传递外部参数,最后是以启动脚本的形式间接进行传递,这样对于命令行的参数解析工作将集中到shell脚本中,大大增加C代码的可移植性.       sh ...

  4. Linux C程序命令行传参

    在命令行环境下,执行已编译的程序时,将命令行参数以同一行的附加参数的形式传入到要执行的程序中.C编译器允许main()函数没有参数,或者有两个参数(也有可能更多,是对标准的扩展).一般形式为" ...

  5. Angular使用@Input和@Output实现父子组件互相传参(类似Vue的props和this.emit)

    app.component.html <app-in-out [in]='"传输进入"' (out)="out($event)" ></app ...

  6. Vue子组件调用父组件方法并传参的5种方式:$emit触发、传入子组件function、访问父组件$parent.function、用inject关联父组件provide的方法、用window.fun

    如需了解老子怎么控制儿子的,传送门:https://s-z-q.blog.csdn.net/article/details/119922715 子组件child.vue <template> ...

  7. 几个经常用到的angular路由Router、ActivatedRoute 知识点:嵌套路由、路由跳转、路由传参、路由参数获取

    深度玩家可移步Angular - 常见路由任务 1.嵌套路由 const routes: Routes = [{path: 'first',component: FirstComponent,//同步 ...

  8. uboot引导kernel - 3 -uboot给内核传参详解

    uboot中执行theKernel函数后,kernel正式启动.如下函数,我们发现有3个参数. 1. 参数 0: 2. 参数machid; 如下code 中获取machid, gd是个全局变量. 2. ...

  9. 函数传参涉及到副本的创建与拷贝问题分析

    遇到一个问题,是这样的: b = [1, 2, 3]def aaa(b):b.append(4)def bbb(b):b = 5aaa(b) print(b) # [1, 2, 3, 4] bbb(b ...

最新文章

  1. 报错You may use special comments to disable some warnings.vue-cli脚手架关闭eslint的步骤
  2. 两条波浪线符号_四年级数学上册第二单元“线的认识”作业单(附带答案)
  3. 秦川团队《科学》刊发研究:新冠感染恒河猴康复后不会再感染
  4. C语言核心技术-C语言概述与开发环境搭建
  5. webpack项目搭建
  6. accept - 指示打印系统接受发往指定目标打印机的打印 任务
  7. 【2019数学建模】国赛C题:机场出租车优化问题(原创)
  8. oracle varchar2 转换成date,将Oracle VARCHAR2转换为DATE并排除无效数据
  9. 苹方字体 for linux,使用macOS苹方替换Windows 10微软雅黑
  10. 算法导论16.2-2
  11. 计算机科学 贺楠,计算机学部-黑龙江东方学院.DOC
  12. WebApp - 微信浏览器解决安全提示“防盗号或诈骗,请不要输入QQ密码”
  13. 西游记中的第一神器是什么?
  14. 天兔(Lepus)监控系统快速安装部署
  15. JS引擎V8的内存回收机制与内存限制(标记清除法)
  16. 计算机蓝屏代码0x000000ED,电脑蓝屏代码0x000000ed解决步骤
  17. App是什么,可以分为几类?及其相关解释。
  18. Web前端期末大作业-在线手机商城网站设计(HTML+CSS+JS)
  19. 微信iOS卡顿监控系统
  20. Python数据分析(matplotlib、numpy、pandas)

热门文章

  1. iOS-指纹识别,面部识别
  2. 基于Javamail的邮件收发系统(系统+论文+开题报告+任务书+外文翻译+文献综述+答辩PPT)
  3. C++优雅地开启/暂停/停止线程——基于观察者模式
  4. JavaScript替换RUI中的参数
  5. pinia的简单用法
  6. Vim中设置空格(space)代替tab键
  7. Java前后端分离动态国际化(动态配置扩展性高)
  8. 红旗Linux桌面4.1文本布置过程图解(一)
  9. 四大力量颠覆软件行业
  10. 【C/C++】宏函数与内联函数的区别