The move to expose only respondJSON()
seems to align with a design philosophy aimed at maintaining consistency by standardizing all responses to use JSON as the format and also to ensure simplification by avoiding duplication of functionality like status and message handling. I think, by promoting respondJSON()
, the intention is likely to ensure that responses are both predictable and extensible.
The concern of respondStatus()
to be made public? depends on the long-term goals. If the intent is to strictly enforce JSON responses, then exposing respondStatus()
might conflict with the above design philosophy. However, making it public could ease the migration process for extensions that rely on legacy HttpUtilities
methods.
As a middle ground, it might be helpful to temporarily introduce respondStatus()
as a wrapper around respondJSON()
to assist in migration, for example:
public static void respondStatus(HttpServletResponse response, String status, String message) {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST); // Example status
respondJSON(response, Map.of("status", status, "message", message));
}
To migrate away from HttpUtilities
, adapting code to use respondJSON()
in combination with appropriate HTTP status codes (SC_*
) might be useful. For example:
if (databaseQueryInfo == null) {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST); // appropriate status
Command.respondJSON(response, Map.of("status", "error", "message", "Invalid or missing Query Info"));
}
Also, we can use the approach of Custom Wrapper Utility
. Instead of calling setStatus()
and respondJSON()
separately, we can encapsulate both into a single reusable method:
public static void respondError(HttpServletResponse response, int statusCode, String errorMessage) {
response.setStatus(statusCode);
Command.respondJSON(response, Map.of("status", "error", "message", errorMessage));
}
Usage:
if (databaseQueryInfo == null) {
respondError(response, HttpServletResponse.SC_BAD_REQUEST, "Invalid or missing Query Info");
}
Thanks!
-Jay