Explain Codes LogoExplain Codes Logo

Does Java have a complete enum for HTTP response codes?

java
http-status-codes
enum
best-practices
Anton ShumikhinbyAnton Shumikhin·Nov 15, 2024
TLDR

No, Java does not feature an all-inclusive enum for HTTP response codes. You can, however, utilize Spring's HttpStatus for an extensive list:

// Using Spring HttpStatus System.out.println(HttpStatus.NOT_FOUND.value()); // Returns 404: Access denied... you could use a break!

For more fundamental codes, use HttpURLConnection:

// Using HttpURLConnection System.out.println(HttpURLConnection.HTTP_NOT_FOUND); // Returns 404: I guess it's time to play hide and seek!

For broader sets, inspect Apache HttpClient or Guava.

Deep dive

Leveraging Apache HttpComponents

Despite the lack of an extensive built-in enum, Apache HttpComponents provides a great alternative. Specifically, the org.apache.http.HttpStatus class houses constants representing all HTTP response codes:

// Using Apache HttpStatus System.out.println(HttpStatus.SC_NOT_FOUND); // Prints 404: Are we not there yet?

However, please note that Apache HttpClient's HttpStatus is considered obsolete.

Using Spring for HTTP status codes

Spring Framework offers a detailed enum org.springframework.http.HttpStatus. It's often updated and more exhaustive than standard Java classes:

// Using Spring HttpStatus HttpStatus status = HttpStatus.valueOf(404); System.out.println(status.getReasonPhrase()); // Prints "Not Found": Didn't Waldo say something about hiding here?

Harnessing HttpServletResponse in Servlet API

Working with the servlet environment? The javax.servlet.http.HttpServletResponse interface should be your go-to. It contains all HTTP response codes as int constants and supports conversions:

// Using HttpServletResponse System.out.println(HttpServletResponse.SC_NOT_FOUND); // Prints 404: feel like we've been here before!

Building a custom HttpStatusCode enum

If you're a code wizard, create your own HttpStatusCode enum. Have a getByValue() method to retrieve statuses and override toString() for displaying status code descriptions:

public enum HttpStatusCode { OK(200, "OK"), NOT_FOUND(404, "Not Found"); // ... add more statuses here private final int value; private final String reason; HttpStatusCode(int value, String reason) { this.value = value; this.reason = reason; } public static HttpStatusCode getByValue(int value) { // Make magic by finding status with int value } @Override public String toString() { return this.value + " " + this.reason; // Abracadabra! It's status time! } }

This custom enum ensures alignment with the official HTTP status code registry.

Best practices and additional resources

Considering servlet specifics

Given servlets are a staple in web applications, you'll often interact with HttpServletResponse. Armed with most standard web communication statuses, it's a reliable reference point.

Staying updated

The Hypertext Transfer Protocol (HTTP) Status Code Registry is an ideal source for up-to-date HTTP codes.

Utilizing official documentation

The Java EE 7 API documentation for javax.ws.rs.core.Response.Status and the Spring documentation for org.springframework.http.HttpStatus provide valuable insights, including extra context to the enum definitions.