Spring Cloud actual combat and Thinking (ii) uploading multiple files via fiegn between Micro Services 1

Source: Internet
Author: User
Tags object object

Requirements Scenario:

    1. The call interface between microservices uploads multiple files at once.
    2. Additional parameters are included with the upload file.
    3. Multiple files can be effectively separated for different processing.

the interface calls between Spring Cloud's microservices use feign. The original feign does not support the transfer of files. Need to use the "feign-form" library. But it looks like the "feign-form" library ( at least the 3.0.3 version ) Only single file uploads are supported. An exception is reported when using multi-file parameters in an interface:

Feign.codec.EncodeException:class[Lorg.springframework.web.multipart.MultipartFile; is not a type supported by ThisEncoder. At Feign.codec.encoder$default.encode (Encoder.java:~[feign-core-9.5.0). Jar:na] at Feign.form.FormEncoder.encode (Formencoder.java:) ~[feign-form-3.0.3.jar:3.0.3] at Feign.form.spring.SpringFormEncoder.encode (Springformencoder.java:) ~[feign-form-spring-3.0.3.jar:3.0.3] at feign. Reflectivefeign$buildencodedtemplatefromargs.resolve (Reflectivefeign.java:351) ~[feign-core-9.5.0. Jar:na] at feign. Reflectivefeign$buildtemplatebyresolvingargs.create (Reflectivefeign.java:213) ~[feign-core-9.5.0. Jar:na] at feign. Synchronousmethodhandler.invoke (Synchronousmethodhandler.java:~[feign-core-9.5.0). Jar:na] at feign. Reflectivefeign$feigninvocationhandler.invoke (Reflectivefeign.java:103) ~[feign-core-9.5.0. Jar:na] at Com.sun.proxy. $Proxy 96.insertWithFiles (Unknown Source)~[na:na]

  

after searching online, refer to the blog "79467843" to change the "Springformencoder" class in the "Feign-form" library. Can support multiple file uploads. Here's how it's implemented:

Micro-service providers Controller Interface:

@ResponseBody @requestmapping (Value= "/psts/add/insertwithfiles", method = requestmethod.post)  Public Object Insertwithfiles (@RequestParam ("Baseinfo") String Baseinfo, @RequestPart (value = "Files") multipartfile[] photofiles) {}

Service Consumer Pom references feign-form dependencies:

<dependency>    <groupId>io.github.openfeign.form</groupId>    <artifactId> Feign-form</artifactid>    <version>3.0.3</version></dependency><dependency>    <groupId>io.github.openfeign.form</groupId>    <artifactid>feign-form-spring</ artifactid>    <version>3.0.3</version></dependency><dependency>    <groupid >commons-fileupload</groupId>    <artifactId>commons-fileupload</artifactId>    < Version>1.3.3</version></dependency>

The service consumer declares a "Feignspringformencoder" Class (This class is copied from the "Springformencoder" interface ):

Importfeign. Requesttemplate;Importfeign.codec.EncodeException;ImportFeign.codec.Encoder;ImportFeign.form.ContentType;ImportFeign.form.FormEncoder;ImportFeign.form.MultipartFormContentProcessor;ImportFeign.form.spring.SpringManyMultipartFilesWriter;ImportFeign.form.spring.SpringSingleMultipartFileWriter;ImportOrg.springframework.web.multipart.MultipartFile;ImportJava.lang.reflect.Type;Importjava.util.Collections;ImportJava.util.Map; Public classFeignspringformencoderextendsFormencoder { PublicFeignspringformencoder () { This(NewDefault ()); }     PublicFeignspringformencoder (Encoder delegate) {Super(delegate); Multipartformcontentprocessor Processor= (multipartformcontentprocessor) This. Getcontentprocessor (Contenttype.multipart); Processor.addwriter (NewSpringsinglemultipartfilewriter ()); Processor.addwriter (NewSpringmanymultipartfileswriter ()); } Public voidEncode (Object object, Type bodyType, requesttemplate template)throwsencodeexception {//comment out the original code//if (!bodytype.equals (Multipartfile.class)) {//Super.encode (object, BodyType, template);//} else {//multipartfile file = (multipartfile) object;//map<string, object> data = Collections.singletonmap (File.getname (), Object);//super.encode (data, map_string_wildcard, template);//        }        //Modify the code to the following        if(Bodytype.equals (Multipartfile.class) {multipartfile file=(Multipartfile) object; Map<string, object> 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<string, object> data =Collections.singletonmap ("Files", object); Super. Encode (data, map_string_wildcard, template); return; }        }        Super. Encode (object, bodyType, template); }}

"Feignspringformencoder" as bean provided to the framework instead of " Springformencoder "encode () " interface :

@Configuration  Public class feignmultipartsupportconfig {    @Bean    @Primary    @Scope ("prototype")    public  Encoder Multipartformencoder () {//        return new Springformencoder ();        return New Feignspringformencoder ();    }    @Bean    public  feign. Logger.level Multipartloggerlevel () {        return  feign. Logger.Level.FULL;}    }

The above program test is feasible. So far demand 1"multi-File Upload" and demand 2"non-file type parameter upload" have been satisfied. Here's a look at how to differentiate between files in an array of files. The "Multipartfile" received by the service providerhas two interfaces "GetName ()" and "getoriginalfilename ()" respectively corresponding to the file the "Metadata" name and file original name of the http header. The previous name is fixed as "files" and cannot be used to differentiate files because of the ability to upload files using an array. It appears that you can only differentiate files by agreeing on the original name of the file. However, if these files are uploaded by the user, this requires the user to modify the file name as agreed before uploading the file. This interface is obviously unfriendly to the user. Confined to space, the next micro-blog to explore the solution to this problem.

Spring Cloud actual combat and Thinking (ii) uploading multiple files via fiegn between Micro Services 1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.