Previously, the author wrote "Uploading files using Spring Cloud Feign". Recently, a colleague is connecting with the legacy Struts antique system and needs to use Feign to implement Form form submission. In fact, the steps are similar. The steps attached to this article are considered as an addition to the previous article.
Add dependencies:
<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 Example:
@FeignClient(name = "xxx", url = "http://www.itmuch.com/", configuration = TestFeignClient.FormSupportConfig.class)public interface TestFeignClient { @PostMapping(value = "/test", consumer = {MediaType.APPLICATION_FORM_URLENCODED_VALUE}, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE} ) void post(Map<String, ?> queryParam); class FormSupportConfig { @Autowired private ObjectFactory<HttpMessageConverters> messageConverters; // new a form encoder to support form form submission @Bean public Encoder feignFormEncoder() { return new SpringFormEncoder(new SpringEncoder(messageConverters)); } // enable Feign's log @Bean public Logger.Level logger() { return Logger.Level.FULL; } }}Call example:
@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();}log:
...[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)
From the log, we can see that Feign can use Form form to submit data at this time.
Reference Documents
https://github.com/OpenFeign/feign-form
https://stackoverflow.com/questions/35803093/how-to-post-form-url-encoded-data-with-spring-cloud-feign
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.