Play Framework: unable to read urls from application.conf

I need to configure some URLs for my Play application, so I added them to application.conf :

 application.url="http://www.mydomain.com" application.url.images="http://www.anotherdomain.com" application.url.images.logo="${application.url.images}/logo.png" ... 

Below is the code that I use in my views to access the above elements:

 @(title: String) @import play.api.Play.current <!DOCTYPE html> <html> ... <img src="@{ currrent.configuration.getString("application.url.images.logo") }" /> ... </html> 

Well ... I'm going crazy since whenever I run the application, I always get the following error message:

 /home/j3d/Projects/test-app/conf/application.conf: 14-19: application.url.images.logo has type OBJECT rather than STRING 

Any idea? Am I missing something? Or is this a mistake?

Thank you very much.

+4
source share
1 answer

Configuration in the Configuration Configuration Library used in Play is a JSON-like structure. Dot notation is syntactic sugar for creating nested objects ( { ... } in JSON). For instance:

 application.url="http://example.com" application.images.logo="http://example.com/img/1.png" application.images.header="http://example.com/img/3.png" 

equivalent to the following JSON:

 { "application": { "url": "http://example.com", "images": { "logo": "http://example.com/img/1.png", "header": "http://example.com/img/3.png" } } } 

In your example, you first assign the string application.url , and then try to add keys to it (the url key in application.url.images ), for example, this is a JSON object, not a string. I do not know the exact behavior of Configafe Config in this case and why it does not cause an error immediately when reading the configuration file.

Try changing the hierarchy of configuration keys, i.e.:

 application.url.prefix="http://www.mydomain.com" application.url.images.prefix="http://www.anotherdomain.com" application.url.images.logo="${application.url.images}/logo.png" 

Here application.url will be an object with the keys prefix and images , and application.url.images will be an object with the keys prefix and logo .

+13
source

All Articles