Sending Data using Multipart File Between Micro-services.

Learn how to implement multipart file transfer between micro-services in your own architecture.

Deep Sherathiya
5 min readFeb 10, 2023
Photo by rare-gallery

Understandably, Communication between micro-services was one of the challenging aspects to grasp during the initial learning of micro-services.

This informational article focuses on utilising Spring’s Rest Template to send an HTTP POST Request with a multipart file from one service (API-Gateway) to a remote service. After putting in numerous hours of research and consolidating my efforts, I finally succeeded in sending a multipart file from one service to another.

I’m hoping that this section of the blog will assist readers who are unfamiliar with the subject or are having trouble grasping how multipart communication works between services. There will be a little more weight in this part. Therefore, fasten your seat belts because this will be enjoyable.

What is MultipartFile in Spring Boot ?

A file that has been uploaded as part of a “multipart/form-data” request is represented by the Multipart-file interface, which is a component of the Spring framework in Java.

Here are some of the characteristics of the MultipartFile

It provides methods for accessing information about the file, such as its name, content type, and size. The contents of the file can be read as a stream of bytes, making it easy to process the file as it is being uploaded. MultipartFile also provides convenient methods for transferring the uploaded file to a local file system or storing it in memory as a byte array. It is part of the Spring framework and integrates well with other Spring components.

Now, let’s focus on the more essential aspect.

Execution Statement

I have an API-Gateway and one micro-service here for my application. An API gateway serves as a middle layer between an application’s front end and its micro services. The client submits API requests to the API gateway, which then routes the requests to the relevant micro-service and relays the results to the client.

1. Multipart File Receiver Controller: This is the controller class for my sample micro-service and it has a method for receiving requests in multipart files.
2. API-Gateway Controller: This controller is capable of receiving files from the client, sending request parameters to a micro-service, and returning the response to the client.

Our objective is to figure out how to send multipart file from one service (API-Gateway) to another micro-service. So let’s get our hands dirty on implementation.

1. Multipart File Receiver Controller

Here’s a controller code of sample micro-service:

In above controller class, handleMultipartRequest method simply accept request in multipart file as request param and it will return response based on your business logic.

Also, I have written method called convertMultipartFileToFile which used to convert MultipartFile to File using Apache commons library.

2. API-Gateway Controller

Here, Let’s write API-Gateway Controller and method to implement logic for sending MultipartFile to above mentioned sample micro-service’s controller.

  • Controller

In above controller class, sendMultipartFileToSampleService method accept multipart file from front end or end user and redirect to micro-service.

Here I have redirect request to earlier mentioned Multipart File Receiver Controller as you can see that my redirect URL in above class is as follow:

<ip-address>:<port>/receive-multipartfile/get-multipart-data

As I am sure you have question, What is sendMultipleFile method doing over there?

Let’s find out what sendMultipleFile method is for.

  • Logic Implementation to Send Multipart File

sendMultipartFile method is used to redirect request using rest template from API-Gateway to micro-service with it’s request param/request body and get response back from micro-service.

Now I’m certain you are wondering about the purpose of above used some of classes. Let me explain.

MappingJackson2HttpMessageConverter

A class in the Spring Framework called MappingJackson2HttpMessageConverter gives your application the ability to do serialization and deserialization.

The default supported media types of MappingJackson2HttpMessageConverter are application/json and application/*+json. This means that by default, the converter is configured to handle JSON data with either a application/json or application/*+json content type.

Here we are passing media type as MULTIPART_FORM_DATA so we need to set supported media types as mentioned in above method.

MultipartBodyBuilder

MultipartBodyBuilder is a class in the Spring Framework that provides a fluent API for constructing a multipart/form-data request body. This type of request is commonly used to upload files to a server.

A common use case for MultipartBodyBuilder is in a microservices architecture where you want to transfer a multipart file from one service to another.

As you can see in above method, Object of MultipartBodyBuilder is created and also part builder with multipart file’s resources.

After coding MappingJackson2HttpMessageConverter and MultipartBodyBuilder, We simply use HttpHeaders and HttpEntity to pass in rest template object.

At last, Call exchange method of rest template with passing all needed parameters and you are good to go !!!

Result Showcase

Headers of Request
API call from API-Gateway with 200 response

Finally

In summary, modern systems frequently need to transport multipart files between micro-services. Use of libraries and frameworks that support creating and sending multipart/form-data requests, such as Apache Commons FileUpload or the Spring Framework’s MultipartFile class and MultipartBodyBuilder, can be used to achieve this type of communication.

These tools make it simpler to reliably and effectively deliver and receive files between micro-services. Additionally, because of the high level of abstraction offered by these libraries, developers may easily perform file transfers without having to worry about the specifics of the communication protocol.

Please let me know if I missed anything in this section or if something is still unclear so that I can make this blog and my understanding of micro-services better! On LinkedIn, you can find me at:

Deep Sherathiya - Senior Software Engineer | LinkedIn

I believe that 'Data Speaks Louder Than Words' in this decade and
I am mad to listen to what data speaks.

www.linkedin.com

--

--

Deep Sherathiya

Bridging the gap between data and applications as a skilled Data Engineer and Software Engineer.