Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use a value for a @RequestMapping that is a String but not a String literal?

Is there a way to use an Enum value in a RequestMapping?

@RequestMapping(value = "/example", 
method = RequestMethod.POST)
public final void foo(final HttpServletResponse response,

I want to use a URL value that is already stored in an Enum.

However, I get compile time errors when I try to put anything except a String literal in the RequestMapping.

How does it know the difference between a String literal and a String that is not a String literal ( not sure what that's called ) ?

This is what I tried but it failed at compile time:

@RequestMapping(value = FooEnum.Example.getStringValue(), 
method = RequestMethod.POST)
public final void foo(final HttpServletResponse response,

I also tried using String.format but it doesn't like that either:

@RequestMapping(value = String.format("%s", FooEnum.Example.getStringValue()), 
method = RequestMethod.POST)
public final void foo(final HttpServletResponse response,
like image 934
Pitra Guden Avatar asked Sep 06 '11 03:09

Pitra Guden


1 Answers

Only literal values can be assigned to annotation attributes because they must be known at compile-time when annotations are processed. Yes, you could use an enum value, where "enum value" is FooEnum.Example, but not on an attribute that takes a String value, and you can't call a method on it.

like image 52
Ryan Stewart Avatar answered Sep 23 '22 22:09

Ryan Stewart