Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot YAML configuration with URL in key no longer loads correctly with version 2

I'm migrating my application from Spring Boot 1.5 to 2.0 and one of the YAML properties no longer loads correctly. The following configuration snippet:

myapp
  serviceUrls:
    'https://example.org/test': 'https://test.example.org/Endpoint'

is mapped to this configuration class:

@ConfigurationProperties(prefix = "myapp", ignoreUnknownFields = false)
public final class MyAppProperties {
  private Map<String, String> serviceUrls = new HashMap<>();
  //[...]
}
  • With Spring Boot 1.5, it loads as a map with https://example.org/test -> https://test.example.org/Endpoint;
  • but with Spring Boot 2.0, the colons and slashes disappear from map keys httpsexample.orgtest -> https://test.example.org/Endpoint.

I couldn't find any mention of this in the migration guide. Has YAML parsing changed in Spring Boot 2? Is there a better way to write YAML maps with URLs as keys?

like image 571
Kolargol00 Avatar asked Jun 18 '18 13:06

Kolargol00


People also ask

What are the different ways to load YAML file in Spring Boot?

Spring Framework provides two convenient classes that can be used to load YAML documents. The YamlPropertiesFactoryBean will load YAML as Properties and the YamlMapFactoryBean will load YAML as a Map.

How do I create an absolute path in YAML?

You may specify both relative and absolute paths to other YAML files using the -l switch, separating multiple files using commas; any settings specified in the -y switch.

What 2 types of file formats can use to inject properties into the spring environment object?

You can use properties files, YAML files, environment variables and command-line arguments to externalize configuration.


1 Answers

I should have checked GitHub issues... someone reported a similar problem. The solution is to use the "bracket syntax", which is unfortunately barely documented, wrapping the keys within brackets:

myapp
  serviceUrls:
    '[https://example.org/test]': 'https://test.example.org/Endpoint'
like image 93
Kolargol00 Avatar answered Sep 28 '22 02:09

Kolargol00