How to write REST APIs for specific environments in Spring Boot?

Let’s say you want to write a REST API in Spring Boot and you also want it to be accessible only in specific environments , say development environment and not in test or production environments.

How can you achieve this in Spring Boot?

By using the @Profile annotation!

Spring Boot has the provision to create separate configuration files for each environment.

For your local environment you can create application-local.yml file under src/main/resources folder and put your local specific configuration inside it (local database details etc)

For your development environment you can create application-dev.yml file and put your development environment specific configuration there.

Similarly for other environments.

And when you deploy your application to each environment , you will ideally activate the environment specific profile through JVM system parameter :

-Dspring.profiles.active=dev

or through various other means.

For our testing purpose we will activate the particular profile by modifying spring.active.profiles property in application.yml file (the master configuration file common to all environments)

Here are the steps to write profile specific APIs:

  1. Create a REST Controller class (Using @RestController annotation)
  2. Annotate it with @Profile annotation with the profile you want passed as a parameter to it.
  3. Write your REST API inside that class
  4. Activate the profile you want and start the application.

That’s it! Now your API can be accessed only in the environment specified by you in @Profile annotation.

Let’s see an example.

I created a simple REST API inside a REST Controller class and gave the profile as dev :

package com.example.profiles;

import org.springframework.context.annotation.Profile;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Profile("dev")
public class TestController {

	
	@GetMapping("/hello")
	public String sayHello() {
		
		return "Hello";
	}
}

The above API can be accessible only in dev environment.

Let’s test it.

Below are the profile specific configuration files I created in my project:

I have two environment specific application yml files , one for local and one for dev.

Let’s activate dev profile in application.yml file :

spring:
  profiles:
    active: dev

Now let’s start the application and try to access the above API:

It works!

Now let me change the active profile to local:

spring:
  profiles:
    active: local

Now let me try to access the API:

It throws 404 error.

And that is what we want!

Now in case if you want your APIs to be accessed in all environments except in one environment you can do it by using ! operator in @Profile annotation.

For example ,

@Profile(“!dev”)

means the REST API will be accessed in all environments except development environment.

That’s it!


Posted

in

by

Comments

Leave a Reply

Discover more from The Full Stack Developer

Subscribe now to keep reading and get access to the full archive.

Continue reading