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");
        }
    }
}

如需相關資訊,請參閱下列資源: