1

I have a spring boot application and want to use an Enum constant to specify the value as shown below, however, the compiler generates an error "Type mismatch: cannot convert from Constants to String" Here is the code block

@RequestMapping(method= RequestMethod.POST, value="/user")
public User createUser(@RequestBody User user,
        @RequestHeader(value= Constants.HEADER_USER_AGENT) String userAgent
)

I know I can use public static String instead of Enum, but I wonder if it is possible to use Enum constants?

Samuel Philipp
  • 10,631
  • 12
  • 36
  • 56
user3310917
  • 405
  • 5
  • 13
  • This is answered in https://stackoverflow.com/questions/13253624/how-to-supply-enum-value-to-an-annotation-from-a-constant-in-java – Chanandler Bong Nov 21 '19 at 10:58

2 Answers2

0

What about

Constants.HEADER_USER_AGENT.toString()

For example? Keep in mind: an enum is not a string, but can be converted into one!

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • tried that too, i was getting "The value for annotation attribute RequestHeader.value must be a constant expression" compiler error – user3310917 Mar 20 '19 at 21:50
0

For example, in your controller.

@GetMapping
public void myEndpoint(@RequestHeader("X-My-Header") EMyEnum myEnum) {
    // ...
}

Example of header request:

X-My-Header: MY_ENUM_VALUE
Gustavo Dias
  • 339
  • 2
  • 6