For example, HttpServletResponse
has the HTTP status codes as constants like
public static final int SC_OK = 200; public static final int SC_CREATED = 201; public static final int SC_BAD_REQUEST = 400; public static final int SC_UNAUTHORIZED = 401; public static final int SC_NOT_FOUND = 404;
Is there any such constants defined for HTTP methods like GET
, POST
, ..., anywhere in the Java EE API so that it could be referenced easily, rather than creating one on my own?
The primary or most commonly-used HTTP methods are POST, GET, PUT, PATCH, and DELETE. These methods correspond to create, read, update, and delete (or CRUD) operations, respectively.
The POST method submits an entity to the specified resource, often causing a change in state or side effects on the server. The PUT method replaces all current representations of the target resource with the request payload. The DELETE method deletes the specified resource.
The public|private static final TYPE NAME = VALUE; pattern is a good way of declaring a constant.
A constant is a variable whose value cannot change once it has been assigned. Java doesn't have built-in support for constants. A constant can make our program more easily read and understood by others. In addition, a constant is cached by the JVM as well as our application, so using a constant can improve performance.
If you are using Spring, you have this enum org.springframework.web.bind.annotation.RequestMethod
public enum RequestMethod { GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, TRACE; }
EDIT : Here is the complete list of constants values in Java 6 You can see that some of those are available in the class HttpMethod but it contains less values than RequestMethod.
public @interface HttpMethod { java.lang.String GET = "GET"; java.lang.String POST = "POST"; java.lang.String PUT = "PUT"; java.lang.String DELETE = "DELETE"; java.lang.String HEAD = "HEAD"; java.lang.String OPTIONS = "OPTIONS"; java.lang.String value(); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With