Any method to get constant for HTTP GET, POST, PUT, DELETE? [duplicate]
Asked Answered
D

1

37

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?

Dostie answered 25/9, 2013 at 15:7 Comment(3)
HttpServlet class declares them but they are private. Write your own, possibly as enums instead.Bowerman
Please, java.net/projects/javaee-spec/pages/JEE (apart from incorrectly using a tag in the title instead of keeping it in the tags)Insecure
@Insecure 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.Xiomaraxiong
X
44

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();
}
Xiomaraxiong answered 25/9, 2013 at 15:9 Comment(2)
That annotation is in the Java EE API stack, but not available in a servlet-api, which I assume is what the OP is asking about.Bowerman
I like complete list of constantsJamilajamill

© 2022 - 2024 — McMap. All rights reserved.