Sarah
Sarah

Reputation: 71

How to calculate time between request and precedent request by rest web service in spring boot?

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 ?

Upvotes: 1

Views: 737

Answers (1)

Eduardo Briguenti Vieira
Eduardo Briguenti Vieira

Reputation: 4579

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;
    }
}

Upvotes: 1

Related Questions