Spring: define @RequestMapping value in a properties file

It should be possible to use placeholders in @RequestMapping, like for example @RequestMapping("${foo.bar}"). Take a look at the documentation for more details:

Patterns in @RequestMapping annotations support ${…​} placeholders against local properties and/or system properties and environment variables. This may be useful in cases where the path a controller is mapped to may need to be customized through configuration. For more information on placeholders, see the javadocs of the PropertyPlaceholderConfigurer class.


As bohuslav burghardt has mentioned this is totally possible.

So if you have a common domain stored in your application.properties file you can use placeholders to call it in your controller/s and even chain placeholders & text together.

For Example...
In your .properties file

app.domain = mydomain/v1

In the controller

@RestController
@RequestMapping("${app.domain}/my-controller")
public class MyController {

Thx for the help. It is my contribution... No dependencies are necessary because maven do everything by itself.

In the property file - use maven interpolation, such as below:

vs= v1

us= users
me= messages

url.user=${vs}/${us}
url.mess=${vs}/${me}

In your destiny file, for example controller/resource (in mycase):

@RestController
//@RequestMapping("v1/users") <<<<<<instead this
@RequestMapping("${url.user}")<<<<<<use this
@Api(value = "API RESTFUL)
public class UserResource {
//