Home > Back-end >  java: cannot find symbol symbol: variable POST
java: cannot find symbol symbol: variable POST

Time:12-28

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

enter image description here

CodePudding user response:

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 yourswitch(method) to an if () - else if () chain or use specific request mappings for the different methods.

  • Related