Languages
[Edit]
EN

Spring Boot 3 - optimal way to send JSON data with binary files in request

6 points
Created by:
Creg
9600

In this article, we would like to show how to optimal way to make POST request with JSON data and binary files in Spring Boot 3.

The JSON format does not allow direct transmission of binary files.

There are available solutions:

  • quite optimal: encode binary files using Base94 (as improvement to Base64) and send them inside JSONs,
  • super optimal: use multipart/form-data as Content-Type and send JSON and binary files inside parts.

The article presents multipart/form-data approach.

Example project

Edit

In the project JSON part has name json and names of file parts use file-* pattern.

The project structure

Edit
Example project to upload binary files with JSON data using Spring Boot 3.
Example project to upload binary files with JSON data using Spring Boot 3.

 

The project files

Edit

src/main/resources/application.properties file:

 

src/main/resources/static/index.html file:

Hint: to distinguish what file is related with what part in JSON data, we can put file names inside JSON data.

 

src/main/java/com/example/demo/UploadController.java file:

 

src/main/java/com/example/demo/DemoApplication.java file:

 

src/main/java/com/example/demo/requests/JsonRequest.java file:

 

src/main/java/com/example/demo/responses/UploadResponse.java file:

 

pom.xml file:

 

The final effect

Edit

Run the application and open http://localhost:8080 to see the effect.

Example application to upload binary files with JSON data in Spring Boot 3.
Example application to upload binary files with JSON data in Spring Boot 3.

 

Alternative titles

  1. Spring Boot 3 - optimal way to upload binary files with JSON data
  2. Spring Boot 3 - optimal way to make POST request with JSON data and binary files
  3. Spring Boot 3 - send JSON data and files as multipart
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join