Get custom parameters from application.conf in Akka HTTP

Hi there! I decided to introduce a new type of posts which called a “Quick Tip”. So this post will be the first in that category. It is a common situation when you find some interesting feature (and maybe not only for you) but it is pretty small for creating a full-size article on it and you also don’t have enough time but need to save the info for the future. That’s what “quick tips” are about.

Read some value from application.conf file

In case of using Akka (Akka HTTP or any other part of this great framework) you may need to move some parameters from your code to some external configuration file. Akka by default uses application.conf. Let’s define some simple string value in our config:

As simple as that!

Since Akka implies that you’re using Actors (SIC!) then you probably have an instance of ActorSystem somewhere in your code. And this is great since ActorSystem reads application.conf by default during constructing.

ActorSystem system = ActorSystem.create("customerServer"); // creates a new actor system

After that you can use this trick to extract the parameter:

 String test = system.settings().config().getString("test");
 System.out.println("Test: " + test);

And you’ll see in your console something like:

The output

Well, our ‘test’ param was successfully read!