简体   繁体   中英

How can I count the number of HTTP method calls in my REST API and put it into a database?

I have a simple program that is supposed to get a user from github's API and I want to count the times the method is called. Here is my REST Controller with a GET method (that's the method to be counted):

@RestController
@RequestMapping("/api/v1")
public class UserController {

    private UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @GetMapping("/user/info/{login}")
    public User getUser(@PathVariable String login) throws IOException {
        userService.insertRecord(login);
        return userService.fetchUser(login);
    }
}

My program works (more or less) as it's supposed to, BUT .insertRecord() method does not work at all. It basically does nothing. The method SHOULD check if the database already has a user of the given login. If yes - then it should proceed to update the record and increment the REQUEST_COUNT number by 1. If no - then it should create a new record of a given login and REQUEST_COUNT as 1. The table in my database has only two column - LOGIN and REUQEST_COUNT. But that method literally does nothing, the table in my database remains untouched.

public void insertRecord(String login) {
//this part checks if there is a login like that in the database already
        String sql = "select * from calls where LOGIN = ?";
        PreparedStatement preparedStatement;
        ResultSet resultSet;
        try {
            preparedStatement = getConnection().prepareStatement(sql);
            preparedStatement.setString(1, login);
            resultSet = preparedStatement.executeQuery();
//if there's a result - I am trying to increment the value of REQUEST_COUNT column by 1.
            if (resultSet.next()) {
                String updateSql = "update calls set REQUEST_COUNT = REQUEST_COUNT + 1 where login = ?";
                PreparedStatement updatePreparedStatement;
                try {
                    updatePreparedStatement = getConnection().prepareStatement(updateSql);
                    updatePreparedStatement.setString(1, login);
                } catch (SQLException e) {
                    logger.error("Could not insert a record into the database.");
                    e.printStackTrace();
                }
//if there is no result - I want to insert a new record.
            } else {
                String insertSql = "insert into calls (LOGIN, REQUEST_COUNT) values (?, ?)";
                try (final PreparedStatement insertPreparedStatement = getConnection().prepareStatement(insertSql)) {
                    insertPreparedStatement.setString(1, login);
                    insertPreparedStatement.setInt(2, 1);
                } catch (SQLException e) {
                    logger.error("Could not insert a record into the database.");
                    e.printStackTrace();
                }
            }

        } catch (SQLException e) {
            logger.error("Operation failed.");
            e.printStackTrace();
        }
    }

No Logger's messages are printed either, it's as if the method was simply completely ignored.

Please, help.

Because you are not calling updatePreparedStatement.executeUpdate() !

The sql will only take effetc after you call executeUpdate() .

You can put a filter that will execute before/after the endpoint executed. And in that particular filter, you can also track which endpoint is executed and take appropriate action for any specific endpoint.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM