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# ...
随机推荐
- PING[ARC2]: Heartbeat failed to connect to standby 'gqtzdb_dg'. Error is 16047
Data Guard搭建完毕之后,执行开启归档路径2,结果发现不同步, SQL> alter system set log_archive_dest_state_2=enable; 查看错误日志 ...
- configure -help
http://www.360doc.com/content/14/1215/17/18578054_433158382.shtml http://blog.csdn.net/mociml/articl ...
- 浅谈网络爬虫爬js动态加载网页(一)
由于别的项目组在做舆情的预言项目,我手头正好没有什么项目,突然心血来潮想研究一下爬虫.分析的简单原型.网上查查这方面的资料还真是多,眼睛都看花了.搜了搜对于我这种新手来说,想做一个简单的爬虫程序,所以 ...
- SQL 游标知识整理
游标声明格: declare 游标名称 cursor (游标关键字) for 游标操作对象(select * from 表名称)游标使用: open 游标名称; fetch first from 游标 ...
- asp.net mvc+jquery easyui开发实战教程之网站后台管理系统开发2-Model层建立
上篇(asp.net mvc+jquery easyui开发实战教程之网站后台管理系统开发1-准备工作)文章讲解了开发过程中的准备工作,主要创建了项目数据库及项目,本文主要讲解项目M层的实现,M层这里 ...
- python字典的内建函数
In [70]: test=dict(x=1,y=2,z=3) In [71]: test Out[71]: {'x': 1, 'y': 2, 'z': 3} In [72]: a=['a','b', ...
- Laravel --- Laravel 5.3 队列使用方法
一.设置存储方式 在config/queue.php中查看队列驱动,在.env 设置[QUEUE_DRIVER] 主要介绍数据库驱动 二.数据库驱动 1.修改.env CACHE_DRIVER=fil ...
- mysql批量update操作时出现锁表
https://www.cnblogs.com/wodebudong/articles/7976474.html 最近遇到一件锁表的情况,发现更新的语句where检索的字段,没有建索引,且是批量操作的 ...
- Google Chrome浏览器插件入门开发
--1. 在html文件中引用js 文件 --2.在Google Chrome中开发简单插件 1.首先,简单说明一下在html 中引用js 文件: 将kittenbook.html 和 kittenb ...
- docker安装gitlab-runner
docker安装gitlab-runner docker pull gitlab/gitlab-runner:latest安装gitlab-runner 打开自己搭建的GitLab网站,点击顶栏的Sn ...