sp00m sp00m - 3 months ago 9
Java Question

How to version my API with Spring MVC?

For now, URLs of my API are for example:

/api/users


With Spring MVC:

@RequestMapping("/api/users")


I would like to version this api:

/api-v1.0/users


The best would be to be able to use an SpEL in the
@RequestMapping
annotation, but it is unfortunately not possible:

@RequestMapping("/api-#{appProps['version']}/users")


What are the other options then?

Answer

@RequestMapping resolves from property place holder values. So define a PropertySourcesPlaceHolderConfigurer like below.

<context:property-placeholder location="classpath*:*.properties"/>

Then use the syntax like below.

@RequestMapping("/api-${version}/users")