Method has too many Body parameters openfeign
feign 调用问题,最新版本的feign和旧版本的稍微有一些不一样,具体如下(eureka 作为服务发现与注册 )
依赖:
compile('io.github.openfeign:feign-java8:9.6.0')
compile('org.springframework.cloud:spring-cloud-openfeign-core')
compile('org.springframework.cloud:spring-cloud-starter-openfeign')
feign client 接口,这里统一使用feign的@Param进行注解
import org.springframework.cloud.openfeign.FeignClient; import feign.Headers;
import feign.Param;
import feign.RequestLine; @FeignClient(value = "xxx-service")
public interface xxxServiceClient { @RequestLine("POST /xxx/save")
String saveLog(@Param(value = "data") String data); }
对应xxx-service的具体rest,这里使用spring的@RequestParam 等进行参数的接收
@RequestMapping(value = "/xxx/save", method = { RequestMethod.POST })
public String saveLog(@RequestParam(value = "data", required = false) String data) String status) {
// TODO
return "xxx";
}
feign 基本请求格式
...
@RequestLine("POST /servers")
void post(@Param("serverId") String serverId, @Param("count") int count);
... @RequestLine("GET /servers/{serverId}?count={count}")
void get(@Param("serverId") String serverId, @Param("count") int count);
...
对于post多参数问题,需要使用form的提交格式(某些字段是xml、json之类的参数),普通的提交,head里是
"Content-Type: application/json; charset=UTF-8", "Accept: application/json"
多参数需要使用
"Content-Type: application/x-www-form-urlencoded; charset=UTF-8", "Accept: application/json"
而第二种格式需要feign-form的支持
依赖:
compile('io.github.openfeign.form:feign-form:3.3.0')
compile('io.github.openfeign.form:feign-form-spring:3.3.0')
compile('io.github.openfeign:feign-java8:9.6.0')
compile('org.springframework.cloud:spring-cloud-openfeign-core')
compile('org.springframework.cloud:spring-cloud-starter-openfeign')
feign:
import org.springframework.cloud.openfeign.FeignClient; import feign.Headers;
import feign.Param;
import feign.RequestLine; @FeignClient(value = "whale-service", configuration = EurekaConfiguration.class)
public interface xxxServiceClient { @Headers({ "Content-Type: application/x-www-form-urlencoded; charset=UTF-8", "Accept: application/json" })
@RequestLine("POST /deviceActionLog/save")
String saveLog(@Param(value = "data") String data); }
import org.springframework.cloud.openfeign.FeignClient;
import feign.Headers;
import feign.Param;
import feign.RequestLine; @FeignClient(value = "xxx-service", configuration = EurekaConfiguration.class)
public interface xxxClient { @Headers({ "Content-Type: application/json; charset=UTF-8", "Accept: application/json" })
@RequestLine("GET /xxx/obj?id={id}")
String getById(@Param(value = "id") String deviceId); }
注:若实现xxxclient的接口,则feign首先调用远程服务,远程服务不可用时,降级调用本地实现类
EurekaConfiguration:
import java.util.ArrayList;
import java.util.List; import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.support.SpringDecoder;
import org.springframework.cloud.openfeign.support.SpringEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.HttpMessageConverter; import feign.Contract;
import feign.auth.BasicAuthRequestInterceptor;
import feign.codec.Decoder;
import feign.codec.Encoder;
import feign.form.FormEncoder;
import feign.form.spring.converter.SpringManyMultipartFilesReader; @Configuration
public class EurekaConfiguration { @Bean
public BasicAuthRequestInterceptor basicAuthRequestInterceptor() {
return new BasicAuthRequestInterceptor("admin", "admin");//eureka 获取注册服务使用的账号密码
} // 使用Feign自己的注解,使用springmvc的注解就会报错
@Bean
public Contract feignContract() {
return new feign.Contract.Default();
} @Autowired
private ObjectFactory<HttpMessageConverters> messageConverters; @Bean // spring使用的编码器
@Primary
Encoder feignEncoder() {
// return new SpringEncoder(this.messageConverters);
return new FormEncoder(new SpringEncoder(this.messageConverters));
} @Bean // download使用的解码器
@Qualifier("download")
public Decoder feignDecoder() {
final List<HttpMessageConverter<?>> springConverters = messageConverters.getObject().getConverters();
final List<HttpMessageConverter<?>> decoderConverters = new ArrayList<HttpMessageConverter<?>>(
springConverters.size() + 1); decoderConverters.addAll(springConverters);
decoderConverters.add(new SpringManyMultipartFilesReader(4096));
final HttpMessageConverters httpMessageConverters = new HttpMessageConverters(decoderConverters); return new SpringDecoder(new ObjectFactory<HttpMessageConverters>() {
@Override
public HttpMessageConverters getObject() {
return httpMessageConverters;
}
});
}
}
参考:
https://github.com/OpenFeign/feign
http://sparkgis.com/java/2018/02/feign-method-has-too-many-body-parameters-%E5%8E%9F-feign-method-has-too-many-body-parameters-xixingzhe/
https://yezhwi.github.io/springcloud/2017/12/07/%E5%AE%9E%E8%B7%B5bug%E6%80%BB%E7%BB%93-Feign/
https://blog.csdn.net/liuchuanhong1/article/details/54728681
编码参考:
https://github.com/OpenFeign/feign-form
Method has too many Body parameters openfeign的更多相关文章
- Caused by: java.lang.IllegalStateException: Method has too many Body parameters
feign多参数问题1.1GET方式错误写法 @RequestMapping(value="/test", method=RequestMethod.GET) Model test ...
- SpringCloud Feign报错:Method has too many Body parameters
1.feign多参数问题 1.1GET方式 错误写法 @RequestMapping(value="/test", method=RequestMethod.GET) Model ...
- 【异常】Caused by: java.lang.IllegalStateException: Method has too many Body parameters
出现此异常原因是引文使用feign客户端的时候,参数没有用注解修饰 1.1GET方式错误写法 @RequestMapping(value="/test", method=Reque ...
- OpenFeign远程调用原理
之前对OpenFeign 了解到只用在接口上面打个注解,然后就可以通过内部调用去调用远程地址.研究完Feign生成对象以及代理对象的作用过程之后发现这个过程用到了Spring的好多东西,在之后的过程中 ...
- [Java in NetBeans] Lesson 05. Method/function
这个课程的参考视频和图片来自youtube. 主要学到的知识点有: Define a method:(motivation: write one time, but use it many times ...
- How to create functions that can accept variable number of parameters such as Format
http://www.chami.com/tips/delphi/112696D.html Sometimes it's necessary to pass undefined number of [ ...
- spring boot项目使用swagger-codegen生成服务间调用的jar包
swagger-codegen的github:https://github.com/swagger-api/swagger-codegen 需要的环境:jdk > 1.7 maven > ...
- SpringCloud学习笔记(3):使用Feign实现声明式服务调用
简介 Feign是一个声明式的Web Service客户端,它简化了Web服务客户端的编写操作,相对于Ribbon+RestTemplate的方式,开发者只需通过简单的接口和注解来调用HTTP API ...
- Feign 系列(04)Contract 源码解析
Feign 系列(04)Contract 源码解析 [TOC] Spring Cloud 系列目录(https://www.cnblogs.com/binarylei/p/11563952.html# ...
随机推荐
- QT编译./configure参数的详细解释
可以随便的转载,只要按照规矩走带上咱论坛的链接就好. ======================================全文是按照./configure -help来翻译的========= ...
- 腾讯云直播录制遇到的bug
1.录制方式应用: 初始化方法 [[TXUGCRecordshareInstance] startCameraCustom:param preview:_showPlayerView]; ID ...
- GO方法与接口
Go语言没有沿袭传统面向对象编程中的诸多概念,比如继承.虚函数.构造函数和析构函数.隐藏的this指针等. 方法 Go 语言中同时有函数和方法.方法就是一个包含了接受者(receiver)的函数,re ...
- java多线程之线程安全
线程安全和非线程安全是多线程的经典问题,非线程安全会在多个线程对同一个对象并发访问时发生. 注意1: 非线程安全的问题存在于实例变量中,如果是方法内部的私有变量,则不存在非线程安全问题. 实例变量是对 ...
- spring boot单元测试之RestTemplate(一)
写代码重要,写好的代码准确无误,且符合预期那更是必不可少. spring boot内嵌了专门的单元测试模块——RestTemplate,保证了程序员可以对自己的代码进行及时的测试. 闲言少叙,直接上代 ...
- redission-tomcat:快速实现从单机部署到多机部署
原文地址: http://blog.jboost.cn/2019/06/29/session-redis.html 一些项目初期出于简单快速,都是做单机开发与部署,但是随着业务的扩展或对可用性要求的提 ...
- vue history 模式打包部署在域名的二级目录的配置指南
最近在做项目,需要把项目部署在域名下的二级目录,并且是在用vue-router的history 模式. 我们都知道vue-router 的两种前端基本访问模式 hash 和history .hash ...
- OVS实现VXLAN隔离
一.实验环境 1.准备3个CentOS7 mini版本的虚拟机,每个主机3个网卡.如图: 图中OVS-1.OVS-2.OVS-3分别为三台CentOS7 mini版虚拟机,分别配备3个虚拟网卡.如图中 ...
- HBase 学习之路(九)——HBase容灾与备份
一.前言 本文主要介绍Hbase常用的三种简单的容灾备份方案,即CopyTable.Export/Import.Snapshot.分别介绍如下: 二.CopyTable 2.1 简介 CopyTabl ...
- ZooKeeper学习之路(三)—— Zookeeper常用Shell命令
一.节点增删改查 1.1 启动服务和连接服务 # 启动服务 bin/zkServer.sh start #连接服务 不指定服务地址则默认连接到localhost:2181 zkCli.sh -serv ...