I have a Spring1 @Controller
annotated class with @RequestMapping
annotated methods. I want to reference values for the @RequestMapping
parameters, value
and method
, from another class, rather than hard coding them into the annotation.
Example
Instead of
@Controller
public class MyController {
@RequestMapping(value="my/path", method=RequestMethod.GET)
public String handlePath() {
// etc...
}
}
I want two files,
@Controller
public class MyController {
@RequestMapping(value=Constants.PATH, method=Constants.PATH_METHOD)
public String handlePath() {
// etc...
}
}
and
public class Constants {
public static final String PATH = "my/path";
public static final RequestMethod PATH_METHOD = RequestMethod.GET;
}
Unfortunately, this fails with the following compile-time error:
error: an enum annotation value must be an enum constant
@RequestMapping(value=Constants.PATH, method=Constants.PATH_METHOD)
^
Question
Why does this work in the case of String
but does not work for enum
s?
Notes
- This question is not Spring specific, this is just an (hopefully) accessible example of this issue.
- I happen to be using Java 8