简体   繁体   中英

java: cannot find symbol symbol: variable POST

I have below logic in my Java code

switch (method) {
    case POST:
        // Logic
        break;
    case PUT:
        // Logic
        break;
}

This logic is working when I'm using spring-web version as 5.3.22. I'm getting below issue when I'm using spring-web version as 6.0.2. Please suggest

在此处输入图像描述

The problem is that up to Spring version 5.3.x, HttpMethod used to be an enum ( https://github.com/spring-projects/spring-framework/blob/5.3.x/spring-web/src/main/java/org/springframework/http/HttpMethod.java#L33 ):

 public enum HttpMethod {

But they changed in in Spring Version 6.x to a "normal" class ( https://github.com/spring-projects/spring-framework/blob/main/spring-web/src/main/java/org/springframework/http/HttpMethod.java#L37 ):

 public final class HttpMethod implements Comparable<HttpMethod>, Serializable {

The reason for this change is given as

Refactor HTTP Method from Enum to Class

This commit refactors HttpMethod from a Java enum into a class. The underlying reason being that HTTP methods are not enumerable, but instead an open range and not limited to the predefined values in the specifications.


While you can use enum constants in a switch statement, you cannot use instances of normal classes.

Therefore if you want to use Spring version 6.x you must rewrite your switch(method) to an if () - else if () chain or use specific request mappings for the different methods.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM