服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - 使用FeignClient调用POST表单Body内没有参数问题

使用FeignClient调用POST表单Body内没有参数问题

2022-10-13 13:12五岁程序员 Java教程

这篇文章主要介绍了使用FeignClient调用POST表单Body内没有参数问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

FeignClient调用POST表单Body没有参数

1、问题:在使用FeignClient调用第三方接口,对方是普通POST接口,但是调用返回400,后发现请求体没有携带需要的参数。

2、查找资料发现,FeignClient默认POST请求发送参数为JSON格式,如需要使用表单形式,必须自定义设置。

3、附代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import com.lenovo.mt.config.FormFeignConfiguration;
import com.lenovo.mt.remote.eservice.vo.EServiceToken;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
/**
 * @Description
 * @Author zhaoke
 * @Date 2019/12/16 10:42
 **/
@FeignClient(name = "***", url = "${***}",configuration = FormFeignConfiguration.class, fallback = TokenFail.class)
public interface ITokenRemote {
    @PostMapping(value = "***",consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    EServiceToken getToken(@RequestParam("client_id")String clientId,@RequestParam("client_secret")String clientSecret,@RequestParam("grant_type")String grantType);
}

其中主要设置在FormFeignConfiguration这个类中

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import feign.codec.Encoder;
import feign.form.FormEncoder;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
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.context.annotation.Scope;
import static org.springframework.beans.factory.config.BeanDefinition.SCOPE_PROTOTYPE;
/**
 * @Description
 * @Author zhaoke
 * @Date 2019/12/17 13:05
 **/
@Configuration
public class FormFeignConfiguration {
    @Autowired
    private ObjectFactory<HttpMessageConverters> messageConverters;
    @Bean
    @Primary
    @Scope(SCOPE_PROTOTYPE)
    Encoder feignFormEncoder() {
        return new FormEncoder(new SpringEncoder(this.messageConverters));
    }
}

FeignClient参数问题

使用Get方式调用服务提供者

服务消费者,需要在所有参数前加上@RequestParam注解

服务消费者,指明是Get方式,如果不指明那么在满足条件1 的情况下默认使用get方式

1和2都满足才使用Get方式

使用Post方式调用服务提供者

在所有参数前面加上@RequestParam注解,并且指明使用的Post方式

在参数前面加上@RequestBody注解(有且只有一个@ RequestBody)或者什么也没有(有多个参数其余参数前面都要加上@RequestParam)

1和2满足一个就使用Post方式

三种情况(无参、单参、多参)

无参Get

如果发送无参的Get请求,可以不指定method,默认就是Get请求

无参Post

指定method是Post方式,否则就是Get请求

单参Get

指定该参数是@RequestParam修饰,这时候method默认是Get,也可以自己指定Get方式

单参Post

该参数前面加@RequestParam修饰,并且使用Post方式

或者

使用@RequestBody修饰该参数,或者没使用@RequestParam和@RequestBody修饰该参数,再这个使用不论指定Post还是Get都会使用Post进行请求

注意:

在使用fegin消费服务的时候,如果参数前面什么也没写,默认是@RequestBody来修饰的

@RequestBody修饰的参数需要以Post方式来传递

在服务提供者的Controller中:

1.如果要接收@RequestBody指明的参数,那么对应方法的对应参数前一定要有@RequestBody;(如果没有的话,收到的参数值就为null;如果写成@RequestParam的话,那么feign调用会失败)

2.如果接收@ RequestParam指明的参数,那么可以写@ RequestParam也可以不写,如果参数名称一致不用,不一致需要写

多参Get

需要在所有参数前面添加@ RequestParam注解,这时候可以加Get也可以不加Get

多参Post

服务消费者中,最多只能有一个参数是@RequestBody指明的,其余的参数必须使用@RequestParam指明

也可以在第一个参数前面什么也不写(默认使用@RequestBody修饰)其余的参数都需要加@RequestParam

如果所有参数都是以@RequestParam修饰,并且使用的是Post方式,那么也是以Post方式来请求

在服务提供者的Controller中:

1.如果服务消费者传过来参数时,全都用的是@RequestParam的话,那么服务提供者的Controller中对应参数前可以写@RequestParam,也可以不写(当两边参数名字一致时,可以省略不写)

2. 如果服务消费者传过来参数时,有@RequestBody的话,那么服务提供者的Controller中对应参数前必须要写@RequestBody(如果是多参数的话,其余参数前视情况可以写@RequestParam,也可以不写)

@RequestBody和@RequestParam的区别

@RequestParam用来处理Content-Type: 为 application/x-www-form-urlencoded编码的内容,在Http中如果不指定Content-Type则默认使用application/x-www-form-urlencoded类型

@ RequestBody,处理HttpEntity传递过来的数据,用来处理非Content-Type: application/x-www-form-urlencoded编码格式的数据

Get请求没有HttpEntity所以不用@ RequestBody

POST请求中,通过HttpEntity传递的参数,必须要在请求头中声明数据的类型Content-Type,SpringMVC通过使用HandlerAdapter 配置的HttpMessageConverters来解析HttpEntity中的数据,然后绑定到相应的bean上

总结:Get不能使用表单,只能在url中传参,传参方式只有这一种。 Post可以使用表单,也可以在url中传参。使用表单时有几种数据类型(表现为数据的存储位置不同):

1、x-www-form-urlencoded 参数存储在query中 用@RequestParam接收。

2、formdata 参数存储在body中,用@RequestBody接收,文件类型用@RequestPart接收。

3、raw(josn,xml) 参数存储在body中 用@RequetBody接收。

总结一下: 凡是放在body中的都可以用@RequestBody接收,文件类型的数据可以用@RequestPart接收。 凡是放在query中的都可以用@RequestParam接收,包括Get方式提交和Post(x-www-form-urlencoded)方式提交的

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qq314499182/article/details/103578593

延伸 · 阅读

精彩推荐