Quick revision
The short answer
When Spring Boot binds map keys, it removes every character that is not a letter, a digit or a dash unless the key is written inside square brackets, so /api/v1 loses its slashes and becomes apiv1. To keep the key exactly, we write it in brackets, and in YAML the bracketed key must be quoted: "[/api/v1]": a. Environment variables cannot express such keys well, so for maps like this we use a config file or SPRING_APPLICATION_JSON.
What to remember
In this answer, we saw that Spring Boot treats map keys as elements of a property name and removes characters other than letters, digits and dashes unless the key is in square brackets, so /api/v1 becomes apiv1. Writing the key as "[/api/v1]" in YAML, or gateway.routes[/api/v1] in a properties file, keeps it exactly. Environment variables cannot express such keys, so a config file or SPRING_APPLICATION_JSON is the way to override them, and a list of objects with a path field avoids the problem altogether.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
When Spring Boot binds map keys, it removes every character that is not a letter, a digit or a dash unless the key is written inside square brackets, so /api/v1 loses its slashes and becomes apiv1. To keep the key exactly, we write it in brackets, and in YAML the bracketed key must be quoted: "[/api/v1]": a. Environment variables cannot express such keys well, so for maps like this we use a config file or SPRING_APPLICATION_JSON.
Why this question is asked
Maps are a natural way to configure routing tables, per-path limits or per-tenant settings, and their keys often contain slashes, dots or colons. The surprise is that the YAML looks right, the application starts, and the map silently contains different keys from the ones we wrote, so lookups such as routes.get("/api/v1") return null. Interviewers ask this because it tests whether we know that property names in Spring Boot have their own rules, separate from YAML. In this answer, we will recap how property names work, reproduce the problem, see why the characters disappear, fix it with brackets, and look at the limits with environment variables.
1A Quick Recap of How Property Names Work
Spring Boot turns every configuration source into flat property names before binding, so YAML nesting becomes dotted names: routes with a child key becomes routes.<key>. A property name is made of elements separated by dots, and each element may contain only lower case letters, digits and dashes in its canonical form. That restriction is what makes relaxed binding possible: Spring Boot can compare names from YAML, properties files and environment variables after normalising them.
A map key is one element of that name. So when we bind a Map<String, String> called routes, each key becomes an element after routes., and it is subject to the same character rules as any other part of a property name.
2Reproducing the Problem
Here is the configuration from the question, placed under a gateway prefix, and a properties class that reads it.
- YAML parses the keythe string "/api/v1"
- Spring Boot builds a property nameroutes + key
- Is the key in square brackets?[/api/v1]YesKeep the key exactly: /api/v1NoDrop characters other than a-z, 0-9 and -
- Bind the map entryapiv1=a without brackets
4 steps in order. Play them, or step through them one at a time.
Notice that the quotes in the YAML did their job: YAML read the key as the string /api/v1. The slashes are lost later, when Spring Boot builds the property name gateway.routes./api/v1 and normalises the element /api/v1 to apiv1 by dropping the characters it does not allow.
3Why the Characters Are Removed
The rule exists so that names from different sources can match. An environment variable can never contain a slash or a dot inside a key, and dots in a name separate elements, so Spring Boot defines a simple canonical form and normalises everything to it. For ordinary property names that is exactly what we want; for map keys that carry meaning in their characters, it is destructive.
Dots show the same problem in a different way. A key such as api.v1, without brackets, is not one key with a dot but two levels, so a Map<String, String> would see a nested structure rather than the key api.v1. Spring Boot's documentation notes that for maps whose values are scalar types, dots in keys can be kept without brackets; check your version, and use brackets whenever the exact key matters.
4The Fix: Brackets Around the Key
Square brackets tell Spring Boot to treat everything inside them as the key, unchanged. In YAML, a key starting with [ would be read as a YAML list, so the bracketed key must be quoted as a string.
So the quotes are for YAML and the brackets are for Spring Boot; we need both. In a .properties file there is no YAML parsing, so the brackets alone are enough.
5What About Environment Variables?
Environment variable names only allow letters, digits and underscores, and Spring Boot reads every underscore as a dot. There is no way to express a slash or brackets, so GATEWAY_ROUTES_APIV1 can only ever produce the key apiv1. If an environment must override such a map, the better options are a mounted config file imported with spring.config.import, or SPRING_APPLICATION_JSON, a single variable holding JSON, where the bracketed key can be written as it is.
| Source | How to write it | Result |
|---|---|---|
| YAML, unquoted brackets | [/api/v1]: a | YAML error or a list, not a key |
| YAML, no brackets | "/api/v1": a | key apiv1 |
| YAML, quoted brackets | "[/api/v1]": a | key /api/v1 |
| properties file | gateway.routes[/api/v1]=a | key /api/v1 |
| environment variable | GATEWAY_ROUTES_APIV1=a | key apiv1 only |
SPRING_APPLICATION_JSON | {"gateway":{"routes":{"[/api/v1]":"a"}}} | key /api/v1 |
For maps that operators change often, a small mounted file is usually the clearest of these: it can be reviewed in Git and keeps the exact keys readable.
6Follow-up questions
Would changing the map value type help?
Not for slashes. Characters such as / are always removed from keys outside brackets. Some types keep dots without brackets when the values are scalars, but relying on that is fragile, so brackets are the reliable answer.
How can I detect this mistake early?
Log or test the bound map. A small test that binds the YAML with ApplicationContextRunner and asserts that routes contains /api/v1 catches it at once, and a startup log of the map keys shows the problem in any environment.
Is a list of objects a better model here?
Often, yes. A list of route entries, each with a path field and a target field, avoids key rules entirely, because the path becomes a value, and values are never normalised. It also makes it easy to add more fields per route later.
7Conclusion
In this answer, we saw that Spring Boot treats map keys as elements of a property name and removes characters other than letters, digits and dashes unless the key is in square brackets, so /api/v1 becomes apiv1. Writing the key as "[/api/v1]" in YAML, or gateway.routes[/api/v1] in a properties file, keeps it exactly. Environment variables cannot express such keys, so a config file or SPRING_APPLICATION_JSON is the way to override them, and a list of objects with a path field avoids the problem altogether.
Open with
YAML itself reads the key correctly as /api/v1. The problem comes afterwards, because Spring Boot turns the YAML into flat property names and the map key becomes one element of the name routes./api/v1.
Then, if they want more
Property name elements only keep lower case letters, digits and dashes, which is what makes relaxed binding across YAML, properties files and environment variables work. So when Spring Boot binds the map, it removes the slashes and the key becomes apiv1, and routes.get of /api/v1 returns null.
The fix is to put the key in square brackets, which tells Spring Boot to keep everything inside exactly as written. In YAML a key starting with a bracket would be parsed as a list, so I also quote it, writing "[/api/v1]": a. In a properties file it is just routes[/api/v1]=a.
Dots in keys have a similar issue, because without brackets a dot creates a new level. Environment variables cannot express these keys at all, since they only allow letters, digits and underscores, and every underscore becomes a dot. So for an override I would use a mounted config file, or SPRING_APPLICATION_JSON with the bracketed key in the JSON.
If the map is really a routing table, I might change the model to a list of objects with a path field, because values are never normalised.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Boot reference
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A map property in YAML, routes: {"/api/v1": a}, binds with key apiv1 instead of /api/v1....": https://scrivio-interview-library.netlify.app/spring-boot/externalized-configuration/e156f4d212/