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的更多相关文章

  1. Caused by: java.lang.IllegalStateException: Method has too many Body parameters

    feign多参数问题1.1GET方式错误写法 @RequestMapping(value="/test", method=RequestMethod.GET) Model test ...

  2. SpringCloud Feign报错:Method has too many Body parameters

    1.feign多参数问题 1.1GET方式 错误写法 @RequestMapping(value="/test", method=RequestMethod.GET) Model ...

  3. 【异常】Caused by: java.lang.IllegalStateException: Method has too many Body parameters

    出现此异常原因是引文使用feign客户端的时候,参数没有用注解修饰 1.1GET方式错误写法 @RequestMapping(value="/test", method=Reque ...

  4. OpenFeign远程调用原理

    之前对OpenFeign 了解到只用在接口上面打个注解,然后就可以通过内部调用去调用远程地址.研究完Feign生成对象以及代理对象的作用过程之后发现这个过程用到了Spring的好多东西,在之后的过程中 ...

  5. [Java in NetBeans] Lesson 05. Method/function

    这个课程的参考视频和图片来自youtube. 主要学到的知识点有: Define a method:(motivation: write one time, but use it many times ...

  6. 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 [ ...

  7. spring boot项目使用swagger-codegen生成服务间调用的jar包

    swagger-codegen的github:https://github.com/swagger-api/swagger-codegen 需要的环境:jdk > 1.7   maven > ...

  8. SpringCloud学习笔记(3):使用Feign实现声明式服务调用

    简介 Feign是一个声明式的Web Service客户端,它简化了Web服务客户端的编写操作,相对于Ribbon+RestTemplate的方式,开发者只需通过简单的接口和注解来调用HTTP API ...

  9. Feign 系列(04)Contract 源码解析

    Feign 系列(04)Contract 源码解析 [TOC] Spring Cloud 系列目录(https://www.cnblogs.com/binarylei/p/11563952.html# ...

随机推荐

  1. 配置QtCreator+CDB远程调试环境(用到了符号表) good

    相关环境信息:开发机Win7 x64.远程机器WinXP.调试器是CDB.Qt版本5.2.1 一.部署远程机器环境 我这里用的是虚拟机(Windows XP),根据你要调试的程序选择安装不同架构的Wi ...

  2. C C++ Java C# JS编译、执行过程的原理入门分析

    C.C++是典型的编译型编程语言,编译链接后,点击则可执行. JS,解释型脚本语言,则不需要进行编译,直接解释执行. Java和C#则是所谓的高级语言,编译执行的方式做了很多处理, 尤其是C#,VS编 ...

  3. Keepalived双主模式配置流程

    实验说明 1)keepalived 支持配置多个VRRP实例,每个实例对应一个业务 2)本次实验将实现 keepalived 的互为主备: 业务A:keepalived01为Master,keepal ...

  4. m3u8解析、转码、下载、合并

    m3u8解析.转码.下载.合并 现在网也上大多数视频需要下载都很麻烦,极少数是MP4,大多都是m3u8, 先说视频下载, pc端: 打开网页,点击视频播放,打开开发者工具,找到网络那一栏, 等整个网页 ...

  5. 微服务SpringCloud之服务注册与发现

    在找.net core 微服务框架时发现了Steeltoe开源项目,它可以基于Spring Cloud实现.net core和.net  Framework的微服务.正好之前也有学习过SpringBo ...

  6. PWN菜鸡入门之栈溢出 (2)—— ret2libc与动态链接库的关系

    准备知识引用自https://www.freebuf.com/articles/rookie/182894.html 0×01 利用思路 ret2libc 这种攻击方式主要是针对 动态链接(Dynam ...

  7. Metasploit实现木马生成、捆绑、免杀

    原创博客,转载请注出处! 我的公众号,正在建设中,欢迎关注: Meatsploit介绍 2018/01/03 更新 Metasploit是一款优秀的开源(!= 完全免费)渗透测试框架平台,在该平台下可 ...

  8. Android开发需要了解的 IM 知识

    引言 即便在通讯如此发达的今天,IM 也依然是诸多场景下非常重要的基础能力.因此做为 一名 Android 开发,不可避免的会遇到一些IM 相关的需求或问题.本文以一个Android开发的角度来讲述I ...

  9. 系统学习 Java IO (六)----管道流 PipedInputStream/PipedOutputStream

    目录:系统学习 Java IO---- 目录,概览 PipedInputStream 类使得可以作为字节流读取管道的内容. 管道是同一 JVM 内的线程之间的通信通道. 使用两个已连接的管道流时,要为 ...

  10. 【docker学习一】CentOS7.5+Docker安装及使用「安装、查看、pull、创建、进入镜像」

    记录安装配置以及使用的过程,可能会有多处摘抄,已注明照抄地址,侵删. 是什么:个人理解,是一种移植性很强的虚拟机,支持版本控制(类似于git),同一个服务器可以运行多个docker容器,每个docke ...