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. mqtt消息推送

    https://github.com/wizinfantry/delphi-mqtt-clienthttps://github.com/Indemsys/Delphi_MQTT_mosquittoht ...

  2. Geoserver发布Image Mossaic图层

    1数据准备:请事先在arcgis desktop软件中将栅格数据拼接完毕,并为每一幅影像生成一个prj文件,坐标系一定是要有的,不然Mossaic图层发布不了. 2."数据存储“->& ...

  3. Python处理图片

    # -*- coding: UTF-8 -*- from PIL import Image import os import sys reload(sys) sys.setdefaultencodin ...

  4. HTML连载11-HTML中被废弃的标签&字符实体

    ​一.为什么会有被废弃的标签 答:HTML中以前存在一部分不是用来添加语义的标签,而与我们HTML标签是用来添加语义的,这与我们的定义不相符. 例如: 1.标签<br>:换行 2.标签&l ...

  5. 前端学习之Bootstrap学习

    一,Bootstrap简介 在前端世界,有个叫Bootstrap的家伙,,是twitter 开源出来的一套前端框架,利用Ta可以快速开发网站界面,它的特点就是比自己从头写简单,直观,方便,快捷,省劲. ...

  6. Zookeeper详解-伪分布式和集群搭建(八)

    说到分布式开发Zookeeper是必须了解和掌握的,分布式消息服务kafka .hbase 到hadoop等分布式大数据处理都会用到Zookeeper,所以在此将Zookeeper作为基础来讲解. Z ...

  7. string类总结第一部分函数介绍

    在前面几章,看了整个String类的源码,给每个方法都行写了注释,但是太过凌乱,今天我就把String类的方法整理归纳,然后再讲一下String类比较难以理解的部分 特此声明:本文篇幅较大,涵盖知识点 ...

  8. 系统学习 Java IO (三)----文件类 File

    目录:系统学习 Java IO---- 目录,概览 Java IO API 中的 File 类可以访问基础文件系统. 使用 File 类,可以: 检查文件或目录是否存在. 如果目录不存在,创建一个目录 ...

  9. 【Zookeeper01】ubuntu下安装zookeeper单例以及集群

    参考链接:http://zookeeper.apache.org/ https://www.cnblogs.com/lyhc/p/6560993.html 系统: 乌班图16.04 虚拟机(zk一般要 ...

  10. 六种 主流ETL 工具的比较(DataPipeline,Kettle,Talend,Informatica,Datax ,Oracle Goldengate)

    六种 主流ETL 工具的比较(DataPipeline,Kettle,Talend,Informatica,Datax ,Oracle Goldengate) 比较维度\产品 DataPipeline ...