Home > Back-end >  I want to version my Spring API, how can I create a global constant for the url it only has to be ch
I want to version my Spring API, how can I create a global constant for the url it only has to be ch

Time:01-09

I have developed a SpringBoot REST API with many controllers, using @RequestMapping annotation. I want to create some kind of global variable to store:

String firstPartUrl = "/api/"

String apiVersion = "v1/"

So that in each controller's request mapping I can put:

@RestController
@RequestMapping(firstPartUrl   apiVersion   "/tubas")
public class TubaController {
}

This way, when the API version increases, I only need to change one value.

Thanks for your help.

CodePudding user response:

Add to application.properties

@RestController
@RequestMapping("${firstPartUrl}"   "${apiVersion}"   "/tubas")
public class TubaController {
}

application.properties

firstPartUrl="/blabla"
apiVersion="/v2"
  •  Tags:  
  • Related