Vishal Sharma
Vishal Sharma

Reputation: 302

How to send large json data to the spring controller

How can i send large volume of json data to a spring controller. Say, i have large json data of about 100k or 1000k records which i need to send to my rest controller in spring or springboot then what is the best/most efficient approach to solve the problem.

I know that the data can be sent using request body but i think sending such a large volume of data in the request body of a REST api is not efficient. I may be wrong here, please correct me if i am.

And the data needs to be stored in the database as quickly as possible. So, i need a quick and reliable approach to the problem.

Upvotes: 1

Views: 8732

Answers (2)

zlaval
zlaval

Reputation: 2027

You can use reactive approach and streaming the data. With Spring, use MediaType.APPLICATION_STREAM_JSON_VALUE producer and Flux as return type. On the client side, subscribe to the stream and process the data, or you can use Spring Batch to save the data to the Database.

Upvotes: 0

Arpan Kanthal
Arpan Kanthal

Reputation: 503

There are two parts to your problem.
1. How to receive such a huge volume: If there is a huge volume of data being received, its generally a good idea to save(from the input stream of the response) it locally as a file and process that data asynchronously.(Make sure you set an appropriately high read timeout, else the data stream might be interrupted ) .
2. How do you process such a huge file: With big files, memory footprint needs to be minimal. For XML , SaxParsers are a golden standard . I found this library which is very similar to sax parsing, but for Json http://rapidjson.org/md_doc_sax.html

Upvotes: 2

Related Questions