Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Spring: Real-time status update to the client over REST API

I am developing a web application in Java Spring where I want the user to be able to upload a CSV file from the front-end and then see the real-time progress of the importing process and after importing he should be able to search individual entries from the imported data.

The importing process would consist of actually uploading the file (sending it via REST API POST request) and then reading it and saving its contents to a database so the user would be able to search from this data.

How could I show the real-time progress of this process? I found a tutorial for jQuery, which shows the progress of amount of data uploaded/transferred, but as the most the work is done while processing the uploaded file, I thought I would like a solution where before the line processing I find out the amount of lines in the file and then the user could see a live message like:

Lines processed: 1 out of 10000

It could update/change incrementally, but as one line is processed pretty quickly, showing each number of lines processed is not that important.

Either way, the question is, what's the easiest way to send these messages from Spring REST API to the client?

like image 761
V. Samma Avatar asked Jul 05 '17 21:07

V. Samma


People also ask

What is Spring Boot for REST API?

Spring Boot is a Java framework, built on top of the Spring, used for developing web applications. It allows you to create REST APIs with minimal configurations. A few benefits of using Spring Boot for your REST APIs include: No requirement for complex XML configurations.

What is REST API and how it works?

REST APIs work like a client-server architecture. The client makes a request and a server (REST API) responds back by providing some kind of data. A client can be any front-end framework like Angular, React, etc, or Spring application ( internal/external ) itself. Data can be sent in various formats like plain text, XML, JSON, etc.

How do I get JSON data from REST API in spring?

It has the HTTP method-specific handler methods. A normal Spring controller is used to retrieve the responses from the ‘RestTemplate’ method and returns a view. This component retrieves the JSON data from the specified URL targeting REST API. Retrieved data is stored in a variable.

Why spring HATEOAS for REST APIs?

And Spring HATEOAS eases building the hypermedia you need to serve to your clients. Throughout this tutorial, you have engaged in various tactics to build REST APIs. As it turns out, REST isn’t just about pretty URIs and returning JSON instead of XML.


1 Answers

I found a solution myself and used Web Sockets for that.

I used this approach from the Spring documentation:

https://spring.io/guides/gs/messaging-stomp-websocket/

It could help on sending the messages for each processed line to the front end listener (after the web socket topic/connection is started) but I used a different approach for the data import, I used batch insert so that was unavailable for me, but web sockets are capable of doing that.

like image 159
V. Samma Avatar answered Sep 21 '22 00:09

V. Samma