Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在RequestMapping值中使用字符串变量_Java_Spring_Spring Boot_Api_Request Mapping - Fatal编程技术网

Java 在RequestMapping值中使用字符串变量

Java 在RequestMapping值中使用字符串变量,java,spring,spring-boot,api,request-mapping,Java,Spring,Spring Boot,Api,Request Mapping,我有以下资料: @Value("${apiVersion") private String apiVersion; @RequestMapping(value = "/{apiVersion}/service/call", method = RequestMethod.POST) 我希望URL是: /apiVersion/service/call 但事实证明,{foo}接受任何值,它实际上并不使用字符串 有没有办法将字符串值用作URL的一部分 编辑

我有以下资料:

@Value("${apiVersion")
private String apiVersion;

@RequestMapping(value = "/{apiVersion}/service/call", method = RequestMethod.POST)
我希望URL是:

/apiVersion/service/call
但事实证明,
{foo}
接受任何值,它实际上并不使用字符串

有没有办法将字符串值用作URL的一部分

编辑

问题是,我有多个电话,我们的价值

@RequestMapping(value = apiVersion + "/call1", method = RequestMethod.POST)

@RequestMapping(value = apiVersion + "/call2", method = RequestMethod.POST)

@RequestMapping(value = apiVersion + "/call3", method = RequestMethod.POST)

etc.
从技术上讲,我可以像你建议的那样为每一个声明常量,但听起来并不是最优的。如果没有办法,那就好了,我只是想知道有没有办法

解决方案

向控制器添加常规映射

@RequestMapping("${apiVersion}")

如果您只想在Java中预定义路径,只需执行以下操作

@RequestMapping(value = foo + "/service/call", method = RequestMethod.POST)
SpringMvc中的PathVariables是端点的占位符,如下所示

@GetMapping(value = "/books/{id}")
public String displayBook(@PathVariable id) { ... }

如果您只想在Java中预定义路径,只需执行以下操作

@RequestMapping(value = foo + "/service/call", method = RequestMethod.POST)
SpringMvc中的PathVariables是端点的占位符,如下所示

@GetMapping(value = "/books/{id}")
public String displayBook(@PathVariable id) { ... }

如果要将其应用于控制器中的所有方法,请在控制器类级别上声明:

@RestController
@RequestMapping("/test")
public class MyController { ...
并且您不需要在方法路径之前预先添加它

否则它应该是常量,如下所示:

private static final String FOO = "test";
并在方法路径前面加上前缀,如:

FOO + "/service/call"

如果要将其应用于控制器中的所有方法,请在控制器类级别上声明:

@RestController
@RequestMapping("/test")
public class MyController { ...
并且您不需要在方法路径之前预先添加它

否则它应该是常量,如下所示:

private static final String FOO = "test";
并在方法路径前面加上前缀,如:

FOO + "/service/call"

我之前尝试过,但是我得到的
元素值必须是一个常量表达式
错误。然后在类中将其定义为最终静态路径:私有静态最终字符串FOO_path=“FOO/service/call”;之后,您可以在@RequestMapping(value=FOO_PATH)编辑的我的问题中使用它来解决您的想法。我之前尝试过,但我得到的
元素值必须是常量表达式
错误。然后在类中将其定义为最终静态路径:private static final String FOO_PATH=“FOO/service/call”;之后,您可以在@RequestMapping(value=FOO_PATH)编辑的我的问题中使用它来解决您的想法。我正在从
application.properties
文件中读取该值,我还可以按照您的建议使用它吗?我编辑了我的问题,以显示实际的声明。很遗憾,我没有。注释中的所有值都必须是常量,并在编译时定义。我见过一些在运行时编辑注释的复杂内容,但我参与的项目只是不使用app.props,而是使用一个包含这些路径的java常量的文件。在app.props以外的另一个文件中,也不应该是这样的问题。
@RequestMapping(${apiVersion}”)
这确实有效。好吧,我学到了一些新东西:)Spring似乎可以在运行时转换占位符,很好,我正在从
应用程序.properties
文件中读取值,我还可以按照你的建议使用它吗?我编辑了我的问题,以显示实际的声明。很遗憾,我没有。注释中的所有值都必须是常量,并在编译时定义。我见过一些在运行时编辑注释的复杂内容,但我参与的项目只是不使用app.props,而是使用一个包含这些路径的java常量的文件。在app.props以外的另一个文件中,也不应该是这样的问题。
@RequestMapping(${apiVersion}”)
这实际上是有效的。好吧,我学到了一些新东西:)Spring似乎可以在运行时转换占位符,很好,