任何获得HTTP GET,POST,PUT,DELETE常量的方法?

day*_*mer 19 java servlets constants http-method

例如,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;
Run Code Online (Sandbox Code Playgroud)

在Java EE API中的任何地方都有为HTTP方法定义的任何此类常量GET,POST以便可以轻松引用它,而不是自己创建一个吗?

Arn*_*lle 25

如果您使用的是Spring,那么就有这个枚举 org.springframework.web.bind.annotation.RequestMethod

public enum RequestMethod {
  GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, TRACE;
}
Run Code Online (Sandbox Code Playgroud)

编辑:以下是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();
}
Run Code Online (Sandbox Code Playgroud)

  • 该注释位于 Java EE API 堆栈中,但在“servlet-api”中不可用,我认为这就是 OP 所询问的内容。 (2认同)