在RequestMapping值中使用String变量 [英] Using a String variable in RequestMapping value
问题描述
我有以下内容:
@Value("${apiVersion")
private String apiVersion;
@RequestMapping(value = "/{apiVersion}/service/call", method = RequestMethod.POST)
我希望URL为:
/apiVersion/service/call
但是事实证明{foo}
接受任何值,实际上并没有使用String.
But it turns out {foo}
accept any value, it doesn't actually use the String.
我是否可以使用String值作为URL的一部分?
Is there a way for me to use the String value as part of the URL?
编辑
问题在于,我有多个有价值的电话.
The issue is that I have multiple calls that us that value.
@RequestMapping(value = apiVersion + "/call1", method = RequestMethod.POST)
@RequestMapping(value = apiVersion + "/call2", method = RequestMethod.POST)
@RequestMapping(value = apiVersion + "/call3", method = RequestMethod.POST)
etc.
从技术上讲,我可以像您建议的那样为每个常量声明常量,但这听起来并不理想.如果没有办法做到这一点,那很好,我只是想知道是否有.
Technically I can declare constants for each one like you suggested, but it doesn't sound optimal. If there is no way to do it then it is fine, I was just wondering if there is.
解决方案
向控制器添加常规映射.
Adding general mapping to the controller.
@RequestMapping("${apiVersion}")
推荐答案
如果要将其应用于控制器中的所有方法,请在控制器类级别上声明它:
If you want to apply it for all methods in a controller declare it on the controller class level:
@RestController
@RequestMapping("/test")
public class MyController { ...
,您无需在方法路径之前添加前缀.
and you do not need to prepend it before method path.
否则它应该是恒定的,例如:
Otherwise it should be constant so like:
private static final String FOO = "test";
并将其放在方法路径之前,如:
and prepend it before method path like:
FOO + "/service/call"
这篇关于在RequestMapping值中使用String变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!