Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prepend some text to all controller URL in Spring

We have some already developed REST APIs in SpringBoot.

Now we want to prepend some text (version of API eg /v1/) to all the @RequestMapping.

Is there any way of doing this except prepending /v1/ to every @RequestMapping


example: Current RequestMapping /employess and /cars/1/driver

Need to build like this /v1/employess and /v1/cars/1/driver

like image 786
Mehraj Malik Avatar asked Feb 04 '26 11:02

Mehraj Malik


1 Answers

You can use such property in your application.properties file:

server.servlet.contextPath=/v1

or you can have a base controller class and extend it with all your controller classes

@RestController
@RequestMapping(value = "${rest.api.version}")
public class MyAbstractController {
}

and store rest.api.version in your application.properties file.

like image 56
Pijotrek Avatar answered Feb 06 '26 00:02

Pijotrek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!