1

I am writing a Rest API using Spring boot. I want to calculate time between a request and the previous request, and if the time is less than 2 min I want to return an object json, trace and black list the elements of the request. How can i do this ?

4
  • Add more details in this question - do you want to track the interval between all requests, requests based on a parameter id, etc. Commented Jul 10, 2020 at 21:51
  • first of all I want to get the time between request and previous request. Commented Jul 10, 2020 at 21:56
  • you can maintain a static Date variable. Store the time of first request and compare with the next request. Commented Jul 10, 2020 at 21:59
  • 1
    can you give me more detail please Commented Jul 10, 2020 at 22:01

1 Answer 1

1

Do something like this:

@RestController @RequestMapping("/test") public class TestController { private static long lastRequest = Long.MAX_VALUE; @RequestMapping("/post") public String postTest() { long currentTime = System.currentTimeMillis(); if (currentTime - lastRequest < 120000){ //DO WHAT YOU WANT } else { lastRequest = currentTime; } } 
Sign up to request clarification or add additional context in comments.

4 Comments

Yes i did the same logic with LocalDateTime.now() Thank you so much Sir
No problem. Just accept the answer if that is correct. Thank you.
Do you have any idea if we want to calculate currentTime - lastRequest for each User(because in your response you are calculating the time between two request for any request)
For each user you will need something to identify the user. REST architecture are normally stateless. What it is possible is the request to pass a header parameter to identify the user. You could create question here on stackoverflow about how to identify the user in a Rest request.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.