add view counter for GET REQUEST

Viewed 31

Im here again to ask help about a little ( i hope is little) problem.

What my friend told me to do, is to add a view/click counter in the GET request and save it into a DATABASE (actualy working with DBEAVER).

I m still looking for a way to do it but i have no idea. can you help pls ? Here the code.

oh btw im using springtools

this is the controller:

@Controller
@RequestMapping("/")
public class RecipeController {
    
//ADMIN- USER
@GetMapping("/user")
public String user() {
    return "user";
}

@GetMapping("/admin")
public String admin() {
    return "admin";
}

and this is the model

private int hitCount; 

public int getHitCount() {
    return hitCount;
}

public void setHitCount(int hitCount) {
    this.hitCount = hitCount;
}

i hope is enough clear, is more info is need im here :D Thx alot

1 Answers

2 options come to my mind :

  1. Create another service-class, which will have a method to track (and update) the API-call count, (you can make DB calls in that method).

  2. Use annotations(AOP) to make a generic handler for tracking the API call counts. Try searching for @Before it comes from org.aspectj.lang.annotation package.

Related