Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use httpclient to upload files in Feign

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you how to use httpclient to upload files in Feign, the content is concise and easy to understand, it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Requirements

The feign-form extension depend on OpenFeign and its concrete versions:

All feign-form releases before 3.5.0 works with OpenFeign 9.* versions

Starting from feign-form's version 3.5.0, the module works with OpenFeign 10.1.0 versions and greater.

IMPORTANT: there is no backward compatibility and no any gurantee that the feign-form's versions after 3.5.0work with OpenFeign before 10.9. OpenFeign was refactored in 10th release, so the best approach-use the freshest OpenFeign and feign-form versions.

Notes:

Spring-cloud-openfeign uses OpenFeign 9.* till v2.0.3.RELEASE and uses 10.* after. Anyway, the dependency already has suitable feign-form version, see dependency pom, so you don't need to specify it separately

Spring-cloud-starter-feign is a deprecated dependency and it always uses the OpenFeign's 9.* versions.

The following version of pom is used. The file upload content under the API httpclient has been tampered with.

Feign-version = 9.6.0 io.github.openfeign.form feign-form 3.4.1 io.github.openfeign.form feign-form-spring 3.4.1 io.github.openfeign feign-httpclient ${feign.version} Io.github.openfeign feign-core ${feign.version} io.github.openfeign feign-hystrix ${feign.version} Io.github.openfeign feign-slf4j ${feign.version} multiple attachment file upload problem

By default, SpringFormEncoder only deals with single files, and a strong turn is done.

Overridepublic void encode (Object object, Type bodyType, RequestTemplate template) throws EncodeException {if (! bodyType.equals (MultipartFile.class)) {super.encode (object, bodyType, template); return;} val file = (MultipartFile) object; val data = singletonMap (file.getName (), object); super.encode (data, MAP_STRING_WILDCARD, template);}

Multiple attachments are supported for uploading files in our system. This needs to be rewritten.

/ * * @ author yugj * @ date 4:27 on 2019-8-28. * / public class FeignSpringFormEncoder extends FormEncoder {/ * * Constructor with the default Feign's encoder as a delegate. * / public FeignSpringFormEncoder () {this (new Default ());} / * * Constructor with specified delegate encoder. * @ param delegate delegate encoder, if this encoder couldn't encode object. * / public FeignSpringFormEncoder (Encoder delegate) {super (delegate); MultipartFormContentProcessor processor = (MultipartFormContentProcessor) getContentProcessor (ContentType.MULTIPART); processor.addWriter (new SpringSingleMultipartFileWriter ()); processor.addWriter (new SpringManyMultipartFilesWriter ());} @ Override public void encode (Object object, Type bodyType, RequestTemplate template) throws EncodeException {if (bodyType.equals (MultipartFile.class)) {MultipartFile file = (MultipartFile) object Map data = Collections.singletonMap (file.getName (), object); super.encode (data, MAP_STRING_WILDCARD, template); return;} else if (bodyType.equals (MultipartFile [] .class)) {MultipartFile [] file = (MultipartFile []) object If (file! = null) {Map data = Collections.singletonMap (file.length = = 0? "": "file", object); super.encode (data, MAP_STRING_WILDCARD, template); return;}} super.encode (object, bodyType, template);}}

The corresponding feignclient can use this configuration.

@ Service@FeignClient (name = "xx-file-sv", fallbackFactory = FileGeneralHystrixFallbackAdapter.class, configuration = FeignSpringFormEncoder.class) public interface IFileClient extends IFile {} the above is how to use httpclient to upload files in Feign. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report