Any method to get constant for HTTP GET, POST, PUT, DELETE?

daydreamer picture daydreamer · Sep 25, 2013 · Viewed 55.5k times · Source

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?

Answer

Arnaud Denoyelle picture Arnaud Denoyelle · Sep 25, 2013

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();
}