使用Feign实现Form表单提交

简介: 原文:http://www.itmuch.com/spring-cloud-sum/feign-form-params/之前,笔者写了《使用Spring Cloud Feign上传文件》。

原文:http://www.itmuch.com/spring-cloud-sum/feign-form-params/

之前,笔者写了《使用Spring Cloud Feign上传文件》。近日,有同事在对接遗留的Struts古董系统,需要使用Feign实现Form表单提交。其实步骤大同小异,本文附上步骤,算是对之前那篇的补充。

  • 添加依赖:

    <dependency>
      <groupId>io.github.openfeign.form</groupId>
      <artifactId>feign-form</artifactId>
      <version>3.2.2</version>
    </dependency>
    <dependency>
      <groupId>io.github.openfeign.form</groupId>
      <artifactId>feign-form-spring</artifactId>
      <version>3.2.2</version>
    </dependency>
    
  • Feign Client示例:

    @FeignClient(name = "xxx", url = "http://www.itmuch.com/", configuration = TestFeignClient.FormSupportConfig.class)
    public interface TestFeignClient {
        @PostMapping(value = "/test",
                consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE},
                produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}
                )
        void post(Map<String, ?> queryParam);
    
        class FormSupportConfig {
            @Autowired
            private ObjectFactory<HttpMessageConverters> messageConverters;
            // new一个form编码器,实现支持form表单提交
            @Bean
            public Encoder feignFormEncoder() {
                return new SpringFormEncoder(new SpringEncoder(messageConverters));
            }
            // 开启Feign的日志
            @Bean
            public Logger.Level logger() {
                return Logger.Level.FULL;
            }
        }
    }
    
  • 调用示例:

    @GetMapping("/user/{id}")
    public User findById(@PathVariable Long id) {
      HashMap<String, String> param = Maps.newHashMap();
      param.put("username","zhangsan");
      param.put("password","pwd");
      this.testFeignClient.post(param);
      return new User();
    }
    
  • 日志:

    ...[TestFeignClient#post] ---> POST http://www.baidu.com/test HTTP/1.1
    ...[TestFeignClient#post] Accept: application/json;charset=UTF-8
    ...[TestFeignClient#post] Content-Type: application/x-www-form-urlencoded; charset=UTF-8
    ...[TestFeignClient#post] Content-Length: 30
    ...[TestFeignClient#post] 
    ...[TestFeignClient#post] password=pwd&username=zhangsan
    ...[TestFeignClient#post] ---> END HTTP (30-byte body)
    

    由日志可知,此时Feign已能使用Form表单方式提交数据。

参考文档

目录
相关文章
|
Ubuntu 网络安全 数据安全/隐私保护
百度搜索:蓝易云【docker通过dockerfile安装sftp教程。】
现在,你已经通过 Dockerfile 成功安装了 SFTP,并且可以使用指定的用户名和公钥进行远程访问。请确保替换示例中的用户名、密码和公钥为自己的实际值。
326 1
|
应用服务中间件 nginx
Nginx的启动、停止与重启
启动  启动代码格式:nginx安装目录地址 -c nginx配置文件地址 例如: [root@LinuxServer sbin]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.
13524 0
|
Ubuntu 开发工具 Python
Ubuntu apt-get和pip国内源更换
Ubuntu apt-get和pip源更换 更新数据源为国内,是为了加速安装包的增加速度。 更换apt-get数据源 输入:sudo -s切换为root超级管理员; 执行命令:vim /etc/apt/sources.
13642 0
|
算法 Java
JAVA 雪花算法 唯一ID生成工具类
JAVA 雪花算法 唯一ID生成工具类
2866 0
|
XML JSON Java
通过 Feign 进行文件上传
通过 Feign 进行文件上传
772 7
|
安全 Java Linux
docker阿里云镜像加速
我们都知道因为某些原因我们访问外网都是比较慢的,比如我们使用maven下载依赖时是一个道理,同样的使用docker从docker.hub上下载镜像也是比较慢的。针对这种访问官网比较慢的情况有两种方案,第一种就是使用国内的仓库,第二种就是使用一个加速器。这里我们配置docker的镜像加速从来来实现提速。
13988 1
docker阿里云镜像加速
|
9月前
|
人工智能 算法 Java
Java高级应用开发:AI赋能下的智能代码生成与优化
本文探讨了AI技术,特别是像DeepSeek这样的智能工具,在Java高级应用开发中的应用。AI在代码生成、优化、自动化测试等方面发挥重要作用,可自动生成高质量代码片段、提出优化建议并检测潜在错误,显著提升开发效率与代码质量。未来,AI将进一步推动Java开发的智能化和自动化,为开发者带来全新的开发体验。
|
Java Maven Android开发
【Azure Developer】VS Code打包Java maven Project 遇见 BUILD FAILURE
Unknown lifecycle phase "lean". You must specify a valid lifecycle phase or a goal in the format <plugin-prefix>:<goal> or <plugin-group-id>:<plugin-artifact-id>[:<plugin-version>]:<goal>
249 5
|
安全 Java 数据库
后端进阶之路——浅谈Spring Security用户、角色、权限和访问规则(三)
后端进阶之路——浅谈Spring Security用户、角色、权限和访问规则(三)
|
Java 编译器 Spring
面试突击78:@Autowired 和 @Resource 有什么区别?
面试突击78:@Autowired 和 @Resource 有什么区别?
16177 6

热门文章

最新文章