Spring Cloud使用Feign实现Form表单提交的示例
程序员文章站
2022-10-10 17:00:25
之前,笔者写了《使用spring cloud feign上传文件》。近日,有同事在对接遗留的struts古董系统,需要使用feign实现form表单提交。其实步骤大同小异,...
之前,笔者写了《使用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/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表单方式提交数据。
参考文档
https://github.com/openfeign/feign-form
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。