欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

SpringCloud Feign参数问题及解决方法

时间:2021-03-13 09:49:40|栏目:JAVA代码|点击:

这篇文章主要介绍了SpringCloud Feign参数问题及解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

今天遇到使用Feign调用微服务,传递参数时遇到几个问题

1.无参数

以GET方式请求

服务提供者

@RequestMapping("/hello")
public String Hello(){
  return "hello,provider";
}

服务消费者

@GetMapping("/hello")
String hello();

2.单个参数

(1)GET――@PathVariable

服务提供者

@GetMapping("/test/{name}")
public String test(@PathVariable String name){
  return "hello,"+name;
}

服务消费者

@GetMapping("/test/{name}")
String test(@PathVariable("name") String name);

(2)GET――@RequestParam

服务提供者

@RequestMapping("/test")
public String test(String name){return "hello,"+name;
}

服务消费者

@RequestMapping("/test")
String test(@RequestParam String name);

会遇到报错

RequestParam.value() was empty on parameter 0

解决方法:

  加上注解的描述,修改为

@RequestMapping("/test")
String test(@RequestParam("name") String name);

(3)POST

@RequestBody

不需要注解的描述

@RequestMapping("/test")
String test(@RequestBody String name);

注:

  •   参数前使用了@RequestBody注解的,都以POST方式消费服务
  •   @RequestBody注解的参数,需要POST方式才能传递数据

2.Feign多参数的问题

(1)GET――@PathVariable

服务提供者

@GetMapping("/test/{name}/{xyz}")
public String test(@PathVariable String name,@PathVariable String xyz){
    return "hello,"+name+","+xyz;
}

服务消费者

@GetMapping("/test/{name}/{xyz}")
String test(@PathVariable("name") String name,@PathVariable("xyz") String xyz);

(1)GET――@RequestParam

服务提供者

@RequestMapping("/test")
public String test(String name,Integer type){
  if(type==1){
    return "hello,"+name;
  }else{
    return "hello,provider-"+name;
  }
}

服务消费者

@RequestMapping("/test")
String test(String name, Integer type);

会遇到报错Method has too many Body parameters

说明:

  如果服务消费者传过来参数时,全都用的是@RequestParam的话,那么服务提供者的Controller中对应参数前可以写@RequestParam,也可以不写

  服务消费者feign调用时,在所有参数前加上@RequestParam注解

正确的写法

@RequestMapping("/test")
String test(@RequestParam("name") String name, @RequestParam("type") Integer type);

(2)POST

如果接收方不变

服务消费者

@RequestMapping("/test")
String test(@RequestBody String name, @RequestBody Integer type);

会遇到报错Method has too many Body parameters

服务消费者为

@RequestMapping("/test")
String test(@RequestBody String name, @RequestParam("type") Integer type);

name的值会为null

说明:

如果服务消费者传过来参数,有@RequestBody的话,那么服务提供者的Controller中对应参数前必须要写@RequestBody

正确的写法

服务提供者

@RequestMapping("/test")
  public String test(@RequestBody String name, Integer type){
    if(type==1){
      return "hello,"+name;
    }else{
      return "hello,provider-"+name;
    }
  }

服务消费者正确的写法

@RequestMapping("/test")
String test(@RequestBody String name, @RequestParam("type") Integer type);

可以接收到参数

总结:

  •   请求参数前加上注解@PathVariable、@RequestParam或@RequestBody修饰
  •   可以有多个@RequestParam,但只能有不超过一个@RequestBody
  •   使用@RequestParam注解时必须要在后面加上参数名
  •   @RequestBody用来修饰对象,但是既有@RequestBody也有@RequestParam,那么参数就要放在请求的url中,@RequestBody修饰的就要放在提交对象中
  •   当参数比较复杂时,feign即使声明为get请求也会强行使用post请求

上一篇:java poi解析word的方法

栏    目:JAVA代码

下一篇:Java Swing中的表格(JTable)和树(JTree)组件使用实例

本文标题:SpringCloud Feign参数问题及解决方法

本文地址:http://www.codeinn.net/misctech/80009.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有