0
0
Spring Bootframework~30 mins

Environment-based profiles in Spring Boot - Mini Project: Build & Apply

Choose your learning style9 modes available
Environment-based Profiles in Spring Boot
📖 Scenario: You are building a Spring Boot application that needs to behave differently in development and production environments. Using environment-based profiles helps you switch configurations easily without changing code.
🎯 Goal: Create a Spring Boot application that uses dev and prod profiles to load different greeting messages. You will set up the data, configure the active profile, implement profile-based logic, and complete the application to display the correct message based on the active profile.
📋 What You'll Learn
Create a Map called greetings with keys dev and prod and their respective greeting messages.
Create a String variable called activeProfile to hold the current profile name.
Use if statements to select the greeting message based on activeProfile.
Complete the Spring Boot @RestController with a @GetMapping that returns the correct greeting.
💡 Why This Matters
🌍 Real World
Environment-based profiles let developers easily switch app behavior between development, testing, and production without changing code. This is common in real projects.
💼 Career
Understanding Spring Boot profiles is essential for backend developers to manage configurations and deploy applications safely across different environments.
Progress0 / 4 steps
1
Create the greetings data map
Create a Map<String, String> called greetings with these exact entries: "dev" mapped to "Hello Developer!" and "prod" mapped to "Welcome User!".
Spring Boot
Need a hint?

Use Map.of to create a small immutable map with the given keys and values.

2
Set the active profile variable
Create a String variable called activeProfile and set it to "dev" to simulate the current environment profile.
Spring Boot
Need a hint?

Just assign the string "dev" to the variable activeProfile.

3
Select greeting based on active profile
Create a String variable called message and use if statements to set it to the greeting from greetings map based on activeProfile. If activeProfile is "dev", set message to greetings.get("dev"), else set it to greetings.get("prod").
Spring Boot
Need a hint?

Use activeProfile.equals("dev") to check the profile and assign message accordingly.

4
Complete the Spring Boot controller
Create a Spring Boot @RestController class called GreetingController with a @GetMapping("/greet") method called greet() that returns the message variable. Include all previous code inside the class and method as needed.
Spring Boot
Need a hint?

Wrap your previous code inside a method greet() annotated with @GetMapping("/greet") inside a class annotated with @RestController.