SpringCloud Feign参数问题及解决方法

 更新时间:2019年12月10日 11:12:59   作者:慕尘  
这篇文章主要介绍了SpringCloud Feign参数问题及解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

这篇文章主要介绍了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多线程(三)

    彻底搞懂Java多线程(三)

    这篇文章主要给大家介绍了关于Java面试题之多线程和高并发的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用java具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2021-07-07
  • springBoot+mybatis-plus实现监听mysql数据库的数据增删改

    springBoot+mybatis-plus实现监听mysql数据库的数据增删改

    mybatis-plus技术是简化了繁琐的代码操作,把增删改查的语句都内置了,直接调用就可以实现数据库的增删改查了,这篇文章主要给大家介绍了关于springBoot+mybatis-plus实现监听mysql数据库数据增删改的相关资料,需要的朋友可以参考下
    2024-01-01
  • JavaSE异常Exception处理方法以及自定义

    JavaSE异常Exception处理方法以及自定义

    网络异常exception是指在网络通信过程中出现的异常情况,这些异常可能是由于网络连接不稳定、网络故障、服务器故障、网络拥堵等原因导致的,这篇文章主要给大家介绍了关于JavaSE异常Exception处理方法以及自定义的相关资料,需要的朋友可以参考下
    2024-07-07
  • Java基础教程之HashMap迭代删除使用方法

    Java基础教程之HashMap迭代删除使用方法

    这篇文章主要给大家介绍了关于Java基础教程之HashMap迭代删除使用方法的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Java具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-05-05
  • Spring源码解析之推断构造方法

    Spring源码解析之推断构造方法

    今天给大家带来的是关于Java的相关知识,文章围绕着Spring推断构造方法展开,文中有非常详细的介绍及代码示例,需要的朋友可以参考下
    2021-06-06
  • Spring Boot通过Redis实现防止重复提交

    Spring Boot通过Redis实现防止重复提交

    表单提交是一个非常常见的功能,如果不加控制,容易因为用户的误操作或网络延迟导致同一请求被发送多次,本文主要介绍了Spring Boot通过Redis实现防止重复提交,具有一定的参考价值,感兴趣的可以了解一下
    2024-06-06
  • netty pipeline中的inbound和outbound事件传播分析

    netty pipeline中的inbound和outbound事件传播分析

    这篇文章主要为大家介绍了netty pipeline中的inbound和outbound事件传播分析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-04-04
  • Spring-AOP @AspectJ进阶之如何绑定代理对象

    Spring-AOP @AspectJ进阶之如何绑定代理对象

    这篇文章主要介绍了Spring-AOP @AspectJ进阶之如何绑定代理对象的操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-07-07
  • spring webflux自定义netty 参数解析

    spring webflux自定义netty 参数解析

    这篇文章主要介绍了spring webflux自定义netty 参数解析,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-09-09
  • Java/Android 获取网络重定向文件的真实URL的示例代码

    Java/Android 获取网络重定向文件的真实URL的示例代码

    本篇文章主要介绍了Java/Android 获取网络重定向文件的真实URL的示例代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-11-11

最新评论