2

我有几个@RequestMapping值会在某一天从“/XXX”变为“/V100”。所以我需要在属性中定义它。我已经用谷歌搜索了,并且有使用 application.properties 的方法,但我必须将“/XXX”值保留在用户定义的属性中,如“local.properties”。是否可以@RequestMapping在用户定义的属性上定义值?

@Controller
@RequestMapping("/XXX")
public class MyController {
...
}

**更新:尝试了几个小时并让它工作。

我的属性

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]}"...
4

1 回答 1

2

除了@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.properties是否存在并api.version在其中设置,它都将优先。

此处提供了更多@PropertySource 示例。

于 2016-04-28T13:15:36.780 回答