1. Overview
1.概述
In this tutorial, we’ll look at how to set the @RequestMapping value in a properties file. Also, we’ll use a practical example where we’ll explain all the necessary configurations.
在本教程中,我们将探讨如何在属性文件中设置@RequestMapping值。此外,我们将使用一个实际的例子,在那里我们将解释所有必要的配置。
First, let’s define a basic @RequestMapping and its configuration.
首先,让我们定义一个基本的@RequestMapping和其配置。
2. @RequestMapping Basics
2.@RequestMapping 基础知识
First of all, we’ll create and annotate our class WelcomeController with @RequestMapping to map web requests. This class will allocate our handler method getWelcomeMessage().
首先,我们将用@RequestMapping创建并注解我们的WelcomeController类,以映射网络请求。这个类将分配给我们的处理方法getWelcomeMessage()。
So, let’s define it:
因此,让我们来定义它。
@RestController
@RequestMapping("/welcome")
public class WelcomeController {
@GetMapping
public String getWelcomeMessage() {
return "Welcome to Baeldung!";
}
}
Also, it’s interesting to note we’ll annotate getWelcomeMessage() with @GetMapping to map only the GET requests. As we can see, we’ve used a hardcoded string for the path, statically indicating the path we want to access. With this configuration, we can perfectly access the resource we’re interested in, as we can see below:
此外,值得注意的是,我们将用@GetMapping()来注释getWelcomeMessage(),以便只映射GET请求。正如我们所看到的,我们使用了一个硬编码的字符串作为路径,静态地指出了我们想要访问的路径。通过这种配置,我们可以完美地访问我们感兴趣的资源,正如我们在下面看到的。
curl http://localhost:9006/welcome
> Welcome to Baeldung!
But, what if we want to make the path depend on a configuration parameter? As we’re going to see next, we can make use of the application.properties.
但是,如果我们想让路径取决于一个配置参数呢?正如我们接下来要看到的,我们可以利用application.properties。
3. @RequestMapping and Properties File
3.@RequestMapping和属性文件
First, as we can see in the documentation, patterns in @RequestMapping annotations support ${…} placeholders against local properties and/or system properties and environment variables. So this way, we can link our properties file to our controller.
首先,正如我们在文档中看到的,@RequestMapping 注释中的模式支持针对本地属性和/或系统属性和环境变量的${…}占位符。所以这样一来,我们可以将我们的属性文件与我们的控制器联系起来。
On the one hand, we need to create the properties file itself. We’ll place it in the resources folder and name it as application.properties. Then, we have to create the property with the name of our choice. In our case, we’ll set the name welcome-controller.path and set the value we want as the endpoint of the request. Now, our application.properties look like this:
一方面,我们需要创建属性文件本身。我们将把它放在resources文件夹中,并把它命名为application.properties。然后,我们必须用我们选择的名称来创建属性。在我们的例子中,我们将设置名称为welcome-controller.path,并设置我们想要的值作为请求的端点。现在,我们的application.properties看起来像这样。
welcome-controller.path=welcome
On the other hand, we have to modify the path that we’ve established statically in the @RequestMapping so that it reads the new property that we’ve created:
另一方面,我们必须修改我们在@RequestMapping中静态建立的路径,以便它能读取我们创建的新属性。
@RestController
@RequestMapping("/${welcome-controller.path}")
public class WelcomeController {
@GetMapping
public String getWelcomeMessage() {
return "Welcome to Baeldung!";
}
}
So, in this way, Spring will be able to map the value of the endpoint, and when the user accesses this URL, this method will be in charge of handling it. We can see below how the same message is displayed with the same request:
因此,通过这种方式,Spring将能够映射端点的值,当用户访问这个URL时,这个方法将负责处理。我们可以在下面看到同样的请求是如何显示的。
curl http://localhost:9006/welcome
> Welcome to Baeldung!
4. Conclusion
4.总结
In this short article, we’ve learned how to set the @RequestMapping value in a properties file. In addition, we’ve created a fully functional example that helps us understand the concepts explained.
在这篇短文中,我们已经学会了如何在属性文件中设置@RequestMapping值。此外,我们还创建了一个功能齐全的例子,帮助我们理解所解释的概念。
The complete source code of the article is available over on GitHub.
该文章的完整源代码可在GitHub上获得。