Servlets Servlet API中是否存在任何请求方法枚举?

Servlets Servlet API中是否存在任何请求方法枚举?,servlets,Servlets,还有更优雅的方式吗 if (request.getMethod() == "OPTIONS") { ... } 我的意思是,是否有任何enum或class我可以用来参考 例如: if (request.getContentType().equals(MediaType.APPLICATION_JSON)) { ... } 还有更优雅的方式吗 if (request.getMethod() == "OPTIONS") { ... } if(request.getMetho

还有更优雅的方式吗

if (request.getMethod() == "OPTIONS") {
   ...
}
我的意思是,是否有任何
enum
class
我可以用来参考

例如:

if (request.getContentType().equals(MediaType.APPLICATION_JSON)) {
    ...
}
还有更优雅的方式吗

if (request.getMethod() == "OPTIONS") {
   ...
}
if(request.getMethod()=“OPTIONS”){
... 
}
这甚至不正确。在Java中,必须使用而不是
==
进行比较:

if(“OPTIONS”.equals(request.getMethod()){
... 
}
是否有任何枚举或类可以用于引用

在类中,您将找到HTTP方法的一些常量,但是它们是私有的,因此您将无法使用它们

如果您使用的是JAX-RS,那么可以使用中定义的常量。有为
DELETE
GET
HEAD
选项
POST
PUT
方法定义的常量。在JAX-RS 2.1中,将提供
补丁
的常量

还有更优雅的方式吗

if (request.getMethod() == "OPTIONS") {
   ...
}
if(request.getMethod()=“OPTIONS”){
... 
}
这甚至不正确。在Java中,必须使用而不是
==
进行比较:

if(“OPTIONS”.equals(request.getMethod()){
... 
}
是否有任何枚举或类可以用于引用

在类中,您将找到HTTP方法的一些常量,但是它们是私有的,因此您将无法使用它们


如果您使用的是JAX-RS,那么可以使用中定义的常量。有为
DELETE
GET
HEAD
选项
POST
PUT
方法定义的常量。从JAX-RS 2.1中,将提供
补丁
的常量。

您可以使用
org.springframework.http.HttpMethod
enum,它具有
mathces(String方法)
resolve(String方法)
方法。例如:
HttpMethod.OPTIONS.matches(request.getMethod())
您可以使用
org.springframework.http.HttpMethod
enum,它具有
mathces(String-method)
resolve(String-method)
方法。例如:
HttpMethod.OPTIONS.matches(request.getMethod())