org.springframework.http.HttpMethod 现在是一个类,而不再是一个枚举

该规则标记使用 org.springframework.http.HttpMethod 代码假定它是一个 enum.

Spring Framework 6 中、 org.springframework.http.HttpMethod 已从枚举重构为标准类。 虽然常用常量如 HttpMethod.GETHttpMethod.POST 仍然可用,任何依赖于枚举特定功能的代码都将无法编译或在运行时失败。

开发人员必须将代码移植到与类兼容的替代方案中,例如将 EnumSet<HttpMethod> 以及 Set<HttpMethod>switch 发言 if-else 块。

下面的示例显示了将被标记的无效用法:


import org.springframework.http.HttpMethod;
import java.util.EnumSet;
public class InvalidHttpMethodUsage {
private EnumSet<HttpMethod> methods = EnumSet.of(HttpMethod.GET, HttpMethod.POST);
    public void handle(HttpMethod method) {
    switch (method) {
          case GET:
              System.out.println("GET");
              break;
        }
    }
}

下面的示例显示了有效的迁移用法:


import org.springframework.http.HttpMethod;
import java.util.Set;
public class ValidHttpMethodUsage {
private Set<HttpMethod> methods = Set.of(HttpMethod.GET, HttpMethod.POST);
    public void handle(HttpMethod method) {
    if (HttpMethod.GET.equals(method)) {
     System.out.println("GET");
        }
    }
}

有关更多信息,请参阅以下资源: