我有几个@RequestMapping
值有一天会从“ / XXX”更改为“ / V100”。所以我需要在属性中定义它。我已经搜索过,并且有使用application.properties的方法,但是我必须在用户定义的属性(例如“ local.properties”)中保留“ / XXX”值。是否可以@RequestMapping
在用户定义的属性上定义值?
@Controller
@RequestMapping("/XXX")
public class MyController {
...
}
**更新:尝试了几个小时并使其正常工作。
my.properties
api.version=V100
mvc-context.xml
<context:property-placeholder ignore-unresolvable="true" location="/WEB-INF/config/property/my.properties"/>
控制器
@RequestMapping("/${api.version}")
Tomcat日志
localhost-startStop-1> [2016-04-28 15:01:35.410] [INFO] [RequestMappingHandlerMapping] [534] Mapped "{[/V100/detail],methods=[GET]}"...
除了@JustinB提供的xml解决方案之外,这里还有一个仅注释的解决方案(已通过Spring Boot测试):
@Controller
@PropertySource(value = "classpath:/user.properties", ignoreResourceNotFound = true)
@RequestMapping("/${api.version:}")
public class MyController {
...
}
api.version
从If src / main / resources / user.properties中读取的值(如果存在)。如果文件丢失或未api.version
设置,则默认为空字符串。
请注意,如果api.version
还在application.properties中定义了它,则将优先考虑是否存在并设置了user.propertiesapi.version
。
这里提供@PropertySource的更多示例。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句