Java Servlet API 中的请求方法常量在哪里?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1857646/
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
Where are the request method constants in the Servlet API?
提问by Thilo
I want to write
我想写
if (POST.equals(req.getMethod()))
instead of
代替
if ("POST".equals(req.getMethod()))
but I cannot find the constant definitions in the Servlet API (only looked in HttpServletRequest, where I expected them to be).
但我在 Servlet API 中找不到常量定义(只在 HttpServletRequest 中查看,我希望它们在那里)。
Where are they (I am using lots of libraries, so if someone else defines them, that would also work)?
它们在哪里(我使用了很多库,所以如果其他人定义了它们,那也可以)?
采纳答案by Matt Leidholm
It appears that Java EE 6 added the HTTP method names as constants to the javax.ws.rs.HttpMethod annotation interface. Depending on your setup, they may be available to you.
Java EE 6 似乎将 HTTP 方法名称作为常量添加到 javax.ws.rs.HttpMethod 注释接口。根据您的设置,它们可能对您可用。
http://docs.oracle.com/javaee/6/api/javax/ws/rs/HttpMethod.html
http://docs.oracle.com/javaee/6/api/javax/ws/rs/HttpMethod.html
回答by Matt
As far as I know, there aren't any constants for that particular property. You can check out the full list of constantsto see what is available, though.
据我所知,该特定属性没有任何常量。不过,您可以查看常量的完整列表以查看可用的内容。
Of course, you can always define your own constants if it makes your code easier to write.
当然,如果您的代码更易于编写,您始终可以定义自己的常量。
回答by ZZ Coder
These constants are defined as private in Servlet,
这些常量在 Servlet 中被定义为私有的,
public abstract class HttpServlet extends GenericServlet
implements java.io.Serializable
{
private static final String METHOD_DELETE = "DELETE";
private static final String METHOD_HEAD = "HEAD";
private static final String METHOD_GET = "GET";
private static final String METHOD_OPTIONS = "OPTIONS";
private static final String METHOD_POST = "POST";
private static final String METHOD_PUT = "PUT";
private static final String METHOD_TRACE = "TRACE";
...
It's perfectly fine just using the method name literally.
直接使用方法名称就完全没问题了。
回答by Thilo
Outside of the JDK, Apache Axis has a public constant for POST (but not for any of the other methods):
在 JDK 之外,Apache Axis 有一个用于 POST 的公共常量(但不是用于任何其他方法):
回答by Machaba
If you wonder why there aren't any enums defined for this, that's explained in this question and answer: Why HttpRequest.HttpMethod is string instead of Enum?
如果您想知道为什么没有为此定义任何枚举,请在此问答中进行解释:Why HttpRequest.HttpMethod is string 而不是 Enum?
Bottom line, the HTTP spec does not restrict the set of methods allowed, so additional methods may be used beyond those which are explicitly mentioned in the spec.
最重要的是,HTTP 规范不限制允许的方法集,因此除了规范中明确提到的方法之外,还可以使用其他方法。
回答by Matthias M
In Spring (so outside JDK, too) you can use:
在 Spring(也在 JDK 之外)中,您可以使用:
org.springframework.web.bind.annotation.RequestMethod
This is a enum which provides all HTTP Methods
这是一个提供所有 HTTP 方法的枚举
So you can use RequestMethod.POST.name()
所以你可以使用 RequestMethod.POST.name()