Managing Web Responses with Enums
Using enums to manage HTTP responses can simplify handling different status codes in web applications. This exercise demonstrates creating enums that include additional information like HTTP status codes.
Este ejercicio forma parte del curso
Input/Output and Streams in Java
Instrucciones del ejercicio
- Define a
intparametercodefor theHttpStatusconstructor. - Return field
codewhen methodgetCode()called. - Print
HttpStatuscode for statusOK.
Ejercicio interactivo práctico
Prueba este ejercicio y completa el código de muestra.
public class HttpStatusExample {
enum HttpStatus {
OK(200), NOT_FOUND(404);
private int code;
// Constructor for the HttpStatus with status code
HttpStatus(____ code) {
this.code = code;
}
public int getCode() {
// return the code for specific http status
return ____;
}
}
public static void main(String[] args) {
// Call method to print the status code
System.out.println(HttpStatus.OK.____());
}
}