Java 任何获取 HTTP GET、POST、PUT、DELETE 常量的方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19008597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Any method to get constant for HTTP GET, POST, PUT, DELETE?
提问by daydreamer
For example, HttpServletResponse
has the HTTP status codes as constants like
例如,HttpServletResponse
将 HTTP 状态代码作为常量,如
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?
是否在 Java EE API 的任何位置为 HTTP 方法定义了任何此类常量GET
,POST
以便可以轻松引用,而不是自己创建一个?
采纳答案by Arnaud Denoyelle
If you are using Spring, you have this enum org.springframework.web.bind.annotation.RequestMethod
如果你使用的是 Spring,你有这个枚举 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 6You can see that some of those are available in the class HttpMethodbut it contains less values than RequestMethod.
编辑:这是Java 6 中常量值的完整列表您可以看到其中一些在类HttpMethod中可用,但它包含的值比 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();
}