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# ...
 
随机推荐
- UWP-磁贴初识
			
原文:UWP-磁贴初识 简单的磁贴内容实现,来自 Bob 的视频. 为一个按钮添加点击事件,来更新磁贴. private void ChangeTileContentButton_Click(obje ...
 - 再谈Delphi关机消息拦截 -- 之控制台程序 SetConsoleCtrlHandler(控制台使用回调函数拦截,比较有意思)
			
这里补充一下第一篇文章中提到的拦截关机消息 Delphi消息拦截:http://blog.csdn.net/cwpoint/archive/2011/04/05/6302314.aspx 下面我再介绍 ...
 - 判断本地系统目录下是否存在XML文件,如果不存在就创建一个XMl文件,若存在就在里面执行添加数据
			
这是我为项目中写的一个测试的例子, 假如,您需要这样一个xml文件, <?xml version="1.0" encoding="utf-8"?> ...
 - CMake编译如何解决[-Werror,-Wformat-security] 问题
			
在用Android Studio进行Android开发时,常常采用 java代码调用C++代码,即JNI调用native的开发模式. 在上层build.gradle编译脚本里面可以指定C++代码的编译 ...
 - linux 磁盘控件找到大文件
			
df -lh Used:已经使用的空间 Avail:可以使用的空间 Mounted on:挂载的目录 然后找到大文件 du是linux下用看查看磁盘的命令 下面我们先一个目录的来查看空间占用情况 du ...
 - 修改系统的shell
			
一:修改系统的shell (选用zsh解释器,相对于bash,对它有很好的兼容性,而且功能上更加强大) 1.查看系统中安装的shell cat /etc/shells ...
 - vuejs切换导航条高亮路由高亮做法
			
我的GitHub前端经验总结,喜欢的话请点star✨✨Thanks.:https://github.com/liangfengbo/frontend-develop vuejs导航条高亮我的做法: 用 ...
 - 原创-使用pywinauto进行dotnet的winform程序控制(一)
			
pywinauto自动化控制win32的ui的程序,网上有好多的教程.但是操作dotnet写的winform教程,就少之又少.今天我就来分享我的pywinauto操作dotnet的winform的研究 ...
 - selenium工作原理详解
			
selenium简介 Selenium是一个用于Web应用程序自动化测试工具.Selenium测试直接运行在浏览器中,就像真正的用户在操作一样.支持的浏览器包括IE(7, 8, 9, 10, 11), ...
 - Django之用户认证auth模块使用
			
Auth认证模块 执行数据库迁移的那两条命令时,即使我们没有建表,django是不是也会创建好多张表?我们创建之后去看一下里面的一个叫auth_user表,既然是表,那肯定应该有对应的操作改表的方法 ...