27

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?

Beryllium
  • 12,164
  • 9
  • 52
  • 81
daydreamer
  • 73,989
  • 165
  • 410
  • 667
  • 1
    `HttpServlet` class declares them but they are `private`. Write your own, possibly as enums instead. – Sotirios Delimanolis Sep 25 '13 at 15:09
  • 3
    Please, https://java.net/projects/javaee-spec/pages/JEE (apart from incorrectly using a tag in the title instead of keeping it in the tags) – BalusC Sep 25 '13 at 15:14
  • @BalusC this post tells that is not possible, which was true until Java 5. I took Matt's answer and his link to the full list of constants. I found that those constants actually exist since Java 6. – Arnaud Denoyelle Sep 25 '13 at 15:22

1 Answers1

33

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();
}
Arnaud Denoyelle
  • 25,847
  • 10
  • 73
  • 128