Sambhav Sharma
Sambhav Sharma

Reputation: 5860

Multiple mappings to the same method of a controller in Spring

I am trying to implement API versioning in my Spring application.

So I want to do something like this api_url/{version}/{params}

Lets say I have changed one of the endpoint functions, so I can map the new function to new url with new version. But I want the other unchanged functions to be mapped to both version urls, like api_url/{old-version}/{old-params} and api_url/{new-version}/{old-params}

So if I can implement something like this, it will help me from saving duplication of code in my Java class. How do I do this. Currently @RequestMapping only allows me to specify one value.

Upvotes: 10

Views: 6665

Answers (1)

Jaimie Whiteside
Jaimie Whiteside

Reputation: 1220

You can use comma separated lists in the request mapping annotation.

@RequestMapping(value={"/url/{id}","/url2/{id}"}, method=RequestMethod.GET)

Upvotes: 20

Related Questions