How to fail at startup in Spring Boot 4 if the base-url property is not set?

1 week ago 10
ARTICLE AD BOX

Looking at the code of the HttpServiceClientAutoConfiguration there is the HttpServiceClientProperties which contains all the client configurations. You could create an @Configuration class which obtains the HttpServiceClientProperties and iterates over all the keys/values in the properties to check the presence of the base-url.

@Configuration public class HttpServiceClientValidatingConfig implements InitializingBean { private final HttpServiceClientProperties props; HttpServiceClientValidatingConfig(HttpServiceClientProperties props) { this.props = props; } public void afterPropertiesSet() { var missing = new ArrayList<String>(); for (var prop : props.entrySet() ) { var name = prop.getKey(); var clientProps = prop.getValue(); if (StringUtils.isEmpty(clientProps.getBaseUrl()) { missing.add("spring.http.service-client." + name + ".base-url"); } } if (!missing.isEmpty()) { throw new IllegalStateException("Required properties not set! (" + missing + ")"); } } }

Something along those lines.

Based on my comment, you could probably write something like this as wel.

@Configuration @Validated @HttpServiceClientValidatingConfig.ValidateRequiredProperties public class HttpServiceClientValidatingConfig { private final HttpServiceClientProperties props; HttpServiceClientValidatingConfig(HttpServiceClientProperties props) { this.props = props; } HttpServiceClientProperties getProps() { return props; } @Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Constraint(validatedBy = ValidateRequiredPropertiesValidator.class) @Documented public static @interface ValidateRequiredProperties { String message() default "{jakarta.validation.constraints.NotBlank.message}"; Class<?>[] groups() default {}; Class<? extends Payload>[] payload() default {}; } public static class ValidateRequiredPropertiesValidator implements ConstraintValidator<ValidateRequiredProperties, HttpServiceClientValidatingConfig> { private String message; @Override public void initialize(ValidateRequiredProperties ca) { this.message = ca.message(); } @Override public boolean isValid(HttpServiceClientValidatingConfig httpServiceClientValidatingConfig, ConstraintValidatorContext ctx) { var props = httpServiceClientValidatingConfig.getProps(); var valid = true; for (var prop : props.entrySet()) { var name = prop.getKey(); var value = prop.getValue(); var url = value.getBaseUrl(); if (ObjectUtils.isEmpty(url)) { valid = false; ctx.buildConstraintViolationWithTemplate(this.message) .addPropertyNode("spring.http.service-client." + name + ".base-url") .addConstraintViolation().disableDefaultConstraintViolation(); } } return valid; } } }

This would integrate with the regular validation cycle and handling.

M. Deinum's user avatar

2 Comments

That works fine and has the added advantage that it will work for all defined properties automatically. The solution of Javad Mahdioun is also nice (And it gives a nicer error message), but you have to do it for every client separately.

2025-11-27T14:07:23.16Z+00:00

You can ofcourse change the message or the exception that is being thrown. Another option could be to write a dedicated class level validator which does the validation. This should give you better error messages and probably taps into the FailureAnalyzer feature as well.

2025-11-27T15:08:59.983Z+00:00

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Read Entire Article