spring cloud feign调用service的两种POST传值方式

Posted rage_angel

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring cloud feign调用service的两种POST传值方式相关的知识,希望对你有一定的参考价值。

1、 application/json 包引用
compile("org.springframework.cloud:spring-cloud-starter-openfeign")
feign端调用
@FeignClient(name = "dcv-service")
public interface DCVService 
    @PostMapping(value = "/dcvApi/dcvValidate")
    ViewData domainValidation(AuthInfo authInfo);
service端
@RequestMapping(value = "/dcvValidate", method = RequestMethod.POST)
@ResponseBody
public ViewData dcvValidate(@RequestBody AuthInfo authInfo) 
    ViewData viewData = dcvService.domainValidation(authInfo);
    return viewData;
2、 application/x-www-form-urlencoded 包引用
compile("org.springframework.cloud:spring-cloud-starter-openfeign")
compile group: 'io.github.openfeign.form', name: 'feign-form', version: '3.3.0'
compile group: 'io.github.openfeign.form', name: 'feign-form-spring', version: '3.3.0'

form表单配置

public class CoreFeignConfiguration 
    @Autowired
    private ObjectFactory<HttpMessageConverters> messageConverters;
    // new一个form编码器,实现支持form表单提交
    @Bean
    public Encoder feignFormEncoder() 
        return new SpringFormEncoder(new SpringEncoder(messageConverters));
    
feign调用
@FeignClient(name = "dcv-service", configuration = CoreFeignConfiguration.class)
public interface DCVService 
    @PostMapping(value = "/dcvApi/dcvValidate", consumes = "application/x-www-form-urlencoded")
    ViewData domainValidation(Map<String, ?> authInfo);
service端
@RequestMapping(value = "/dcvValidate", method = RequestMethod.POST)
@ResponseBody
public ViewData dcvValidate(AuthInfo authInfo) 
    ViewData viewData = dcvService.domainValidation(authInfo);
    return viewData;
 

参考:form-url-encode

注1: feign默认的是application/json的方式。

注2: 测试过程中出现 read time out异常,发现默认超时时间是1s,可以配置超时时间如下:

ribbon:
  ReadTimeout: 60000   #default=1000
  ConnectTimeout: 60000  #default=1000
  maxAutoRetries: 0 #调用失败的重试次数

以上是关于spring cloud feign调用service的两种POST传值方式的主要内容,如果未能解决你的问题,请参考以下文章

Spring Cloud Feign 调用过程分析

Spring Cloud Alibaba微服务调用组件Feign原理+实战

Spring Cloud Feign 1(声明式服务调用Feign 简介)

spring cloud feign调用service的两种POST传值方式

spring cloud feign调用service的两种POST传值方式

Spring Cloud之Feign客户端调用工具