Quick revision
The short answer
We bind payments.api-url in a @ConfigurationProperties class annotated with @Validated, and put Bean Validation constraints on the field: @NotNull or @NotBlank for presence and Hibernate Validator's @URL, or a @Pattern, for the format. spring-boot-starter-validation must be on the classpath, nested objects need @Valid for their constraints to be checked, and a failure stops startup with a clear message naming the property, the value and the reason.
What to remember
In this answer, we made startup fail when payments.api-url is missing or invalid by binding it in a @ConfigurationProperties class annotated with @Validated, with @NotBlank for presence and @URL or @Pattern for the format. We saw that spring-boot-starter-validation must be present and nested objects need @Valid, otherwise validation is silently skipped, and that a failure produces a clear message naming the property, the value, its origin and the reason, which stops a rollout at its first pod.
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.
We bind payments.api-url in a @ConfigurationProperties class annotated with @Validated, and put Bean Validation constraints on the field: @NotNull or @NotBlank for presence and Hibernate Validator's @URL, or a @Pattern, for the format. spring-boot-starter-validation must be on the classpath, nested objects need @Valid for their constraints to be checked, and a failure stops startup with a clear message naming the property, the value and the reason.
Why this question is asked
A service that starts with a missing or malformed URL looks healthy until the first payment, which then fails with a confusing connection error, possibly hours after the deployment finished. Failing at startup is far better: the new pods never become ready, the rollout stops, and the old version keeps serving. Interviewers ask this coding question because it shows whether we know how Spring Boot connects configuration binding with Bean Validation, including the details that make it silently not work. In this answer, we will recap configuration validation, write the class, see the startup failures, look at the dependency and nested objects, and compare the URL checks we can use.
1A Quick Recap of Configuration Validation
Bean Validation is the Jakarta standard for declaring rules on fields with annotations such as @NotNull, @NotBlank, @Min and @Pattern; Hibernate Validator is the implementation Spring Boot uses. Spring Boot can run these rules on @ConfigurationProperties objects right after binding them, while the application starts.
Two things must be true for that to happen. The class must be annotated with @Validated, which tells Spring Boot to validate it, and a validator must exist, which means the spring-boot-starter-validation dependency. If either is missing, the constraints are just annotations that nothing reads.
2Writing the Properties Class
Here is the class for the payments prefix. The property api-url in YAML binds to the record component apiUrl through relaxed binding.
Notice that we use two constraints for two different failures. @NotBlank rejects a missing or empty value; @URL checks the format and, with protocol = "https", also rejects plain http. @URL on its own treats null as valid, as almost all Bean Validation constraints do, which is why the presence check is a separate annotation.
3What the Failures Look Like
With the class in place, a missing or wrong value stops the application before any bean that uses it is created. Spring Boot prints a failure analysis that names the property, the bound value, where it came from and the broken rule.
- Bind payments.* to PaymentsPropertiesrelaxed binding from all sources
- Is the class @Validated and a validator present?spring-boot-starter-validationYesRun the constraintsNoSkip validation silently
- Do all constraints pass?
@NotBlank,@URLYesCreate the bean; startup continuesNoAPPLICATION FAILED TO START
3 steps in order. Play them, or step through them one at a time.
The exact layout of the message varies a little between versions, but it always names the property and the reason, and the Origin line tells us where the bad value came from. In Kubernetes, that output is in the logs of a pod in CrashLoopBackOff, and the rollout stops at the first pod.
4The Dependency and Nested Objects
The two silent failures are worth knowing well. Without spring-boot-starter-validation, there is no Bean Validation provider, and Spring Boot cannot validate; depending on the version it may log a warning, but startup continues with the bad value. So the starter belongs in the build of every service that relies on validated configuration.
The second is nesting. @Validated starts validation of the outer object, but Bean Validation only descends into a nested object if the field is marked with @Valid. Without it, the constraints inside Retry are never checked.
5Choosing the URL Check
There are a few ways to check the format, and they differ in strictness. Hibernate Validator's @URL is the most readable and can restrict the protocol, host or port. A @Pattern with a regular expression gives exact control, for example requiring https and a host ending in our domain, at the cost of readability. Binding to java.net.URI instead of String makes Spring Boot parse the value during binding, so a value with illegal characters fails, but URI accepts many strings that are not usable URLs, such as a bare word, so it still needs a constraint.
So a typical choice is @NotBlank with @URL(protocol = "https") for most services, and a pattern when security asks us to allow only known hosts.
6Follow-up questions
Does @Validated work with @Value?
No. Configuration validation applies to @ConfigurationProperties objects. A @Value field has no validation step, which is one of the main reasons to move required values into a properties class.
Why not check the URL in the client constructor instead?
That also fails at startup if the client is created eagerly, but it scatters rules across the code and gives less helpful messages. Constraints on the properties class keep every rule in one place, produce the standard failure analysis with the origin of the value, and can be tested on their own.
How can I test the validation?
With ApplicationContextRunner: run it with withPropertyValues("payments.api-url=not-a-url") and assert that the context has failed. Such tests run in milliseconds and prove the rule works before any deployment.
7Conclusion
In this answer, we made startup fail when payments.api-url is missing or invalid by binding it in a @ConfigurationProperties class annotated with @Validated, with @NotBlank for presence and @URL or @Pattern for the format. We saw that spring-boot-starter-validation must be present and nested objects need @Valid, otherwise validation is silently skipped, and that a failure produces a clear message naming the property, the value, its origin and the reason, which stops a rollout at its first pod.
Open with
I would bind the property in a @ConfigurationProperties class for the payments prefix, a record with a String apiUrl component, which api-url maps to through relaxed binding. I annotate the record with @Validated and put two constraints on the component. @NotBlank catches a missing or empty value, and Hibernate Validator's @URL, here with protocol https, catches a malformed one.
Then, if they want more
I need both, because @URL treats null as valid like most constraints. For validation to run at all, spring-boot-starter-validation has to be on the classpath; without a validator the annotations are silently ignored. If the class has nested groups, like a retry object, I mark that component with @Valid, otherwise its constraints are never checked.
With that in place, a missing or bad value stops the application during startup with APPLICATION FAILED TO START. The message names the property, the value, the origin such as the environment variable, and the reason, like must be a valid URL. In Kubernetes the new pod never becomes ready, so the rollout stops and the old version keeps serving.
If I need stricter rules, like only our own domains, I would use @Pattern or a check in the record's compact constructor. I would also add an ApplicationContextRunner test that sets a bad URL and asserts the context fails.
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 "How do you make startup fail when the required property payments.api-url is missing or...": https://scrivio-interview-library.netlify.app/spring-boot/externalized-configuration/d9fd7bb60f/