Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

November Discount Launch 2022 – TEMP TOP (NPI)
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

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.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

Generic footer banner
Comments are closed on this article!