处理来自一个POST请求的3个不同响应

时间:2017-07-02 20:06:33

标签: java postgresql rest post

我试图更改以下代码,因此它只会生成一个查询而不是两个。问题是,你可以看到我需要返回3个可能的响应。如果没有满足条件,则默认值为404;如果登录凭据正确但帐户已过期,则返回第二个404响应。如果帐户正确且有效,则返回唯一有效的响应(200)。

如何将SELECT语句合并为一个,同时保留以3种不同方式响应的能力,具体取决于用户的状态?

    @POST
    @Path("accounts/initial/{user}")
    @Produces("application/json")
    @Consumes("application/json")
    public Response validate(@PathParam("user") final String username) {

        PreparedStatement statement = null;
        ResultSet rs = null;
        Connection conn = null;
        try {
            conn = LicenseDBSource.getInstance().getConnection();
            statement = conn.prepareStatement("SELECT * FROM members WHERE username = ?");
            statement.setString(1, username);
            rs = statement.executeQuery();

            if (rs.next()) {
                statement = conn.prepareStatement("SELECT * FROM members WHERE
                username = ? AND LOCALTIMESTAMP < expiration");
                statement.setString(1, username);

                rs = statement.executeQuery();
                if (rs.next()) {
                    return Response.status(200).build();
                    }
                } else {
                    return Response.status(404).entity("Expired").build();
                }
            }
        } catch (...) {
           ...handle exceptions...
        } finally {
            closeResources(...);
        }
        return Response.status(404).entity("Incorrect").build();
    }

我的数据库包含的列包括&#34;用户名&#34;和&#34;到期&#34;。

1 个答案:

答案 0 :(得分:1)

如果只想使用一个语句执行两个查询,则可以使用两个子查询。你的尝试块看起来像:

try {
    conn = LicenseDBSource.getInstance().getConnection();
    statement = conn.prepareStatement(
        "SELECT " + 
        "   EXISTS (SELECT * FROM members WHERE username = ?) AS user_exists, " +
        "   EXISTS (SELECT * FROM members WHERE username = ? " + 
                       " AND LOCALTIMESTAMP < expiration) AS user_is_current" ;
    statement.setString(1, username);
    statement.setString(2, username);
    rs = statement.executeQuery();
    rs.next(); // There will *always* be one row
    if (rs.getBoolean("user_exists")) {
        if (rs.getBoolean("user_is_current") {
            return Response.status(200).build();
        } else {
            return Response.status(404).entity("Expired").build();
        }
    }

备用,使用单个查询,但这次您有两个选项:它不返回任何行(意味着用户不存在),或只返回一个 row(假设usrname为PK),带有计算列,告诉您用户是否是最新的。这实际上比以前的方法更清洁,花费更少的时间:

try {
    conn = LicenseDBSource.getInstance().getConnection();
    statement = conn.prepareStatement(
        "SELECT " + 
        "    (LOCALTIMESTAMP < expiration) AS user_is_current " +
        "FROM " +
        "    members WHERE username = ?";

    statement.setString(1, username);
    rs = statement.executeQuery();

    if (rs.next()) {
        if (rs.getBoolean("user_is_current") {
            return Response.status(200).build();
        } else {
            return Response.status(404).entity("Expired").build();
        }
    }
}