openFegin传参

项目之间调用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";
    }
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值