Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

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.

First, let’s define a basic @RequestMapping and its configuration.

2. @RequestMapping Basics

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().

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:

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.

3. @RequestMapping and Properties File

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.

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:

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:

@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:

curl http://localhost:9006/welcome 
> Welcome to Baeldung!

4. Conclusion

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.

The complete source code of the article is available over on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.