我使用@RequestMapping注释开发了一个带有许多控制器的SpringBoot REST API。我想创建某种全局变量来存储:
String firstPartUrl = "/api/"
String apiVersion = "v1/"
这样我就可以在每个控制器的请求映射中放入:
@RestController
@RequestMapping(firstPartUrl + apiVersion + "/tubas")
public class TubaController {
}
这样,当API版本增加时,我只需要更改一个值即可。
感谢您的帮助。
添加到application.properties
@RestController
@RequestMapping("${firstPartUrl}" + "${apiVersion}" + "/tubas")
public class TubaController {
}
应用程序属性
firstPartUrl="/blabla"
apiVersion="/v2"
将以下行添加到 application.properties 中
server.servlet.context-path=/api/v1
它将全局添加“api/v1”作为所有端点的前缀