3

I am running into an issue with unbound properties in my application.yml file. I am in the middle of migrating from Spring Boot 1.5.4 to Spring Boot 2. My problem is that I have some properties which can optionally be left blank, for instance:

application.yml

app:
  enabled: false
  url: #ldap://127.0.0.1:3268
  user: #admin

In this case if ldap.enabled is set to true, then the ldap properties can be set to the needed values which are currently commented out. However, if ldap.enabled is set to false then the rest of the properties are not set and are left blank.

In my Spring Boot 1.5.4 application I didn't have any problems with this, but now after upgrading to Spring Boot 2 I get the following exceptions:

org.springframework.boot.context.properties.bind.UnboundConfigurationPropertiesException: The elements [ldap.ignorecertificate] were left unbound.
org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'ldap' to com.myapp.server.config.properties.LdapProperties

LdapProperties.java

@Component
@ConfigurationProperties(prefix = "app", ignoreUnknownFields = false)
@Getter
@Setter
public class AppProperties {

    private Boolean enabled;
    private String url;
    private String user;

}

I know I can set ignoreUnvalidFields = true in @ConfigurationProperties, but this is not exactly the behavior I want, since an empty value is valid in my case.

Is there a way I can ignore unbound properties? What can I do to avoid this problem?

UPDATE

After further debugging I can see that because ignoreUnkownFields = false in @ConfigurationPropertes then a NoUnboundElementsBindHandler is returned, which checks for unbound properties:

class ConfigurationPropertiesBinder {

    private final ApplicationContext applicationContext;

    private final PropertySources propertySources;

    private final Validator configurationPropertiesValidator;

    private final boolean jsr303Present;

    private volatile Validator jsr303Validator;

    private volatile Binder binder;

    ...
    ...
    ...

    private BindHandler getBindHandler(ConfigurationProperties annotation,
            List<Validator> validators) {
        BindHandler handler = new IgnoreTopLevelConverterNotFoundBindHandler();
        if (annotation.ignoreInvalidFields()) {
            handler = new IgnoreErrorsBindHandler(handler);
        }
        if (!annotation.ignoreUnknownFields()) {
            UnboundElementsSourceFilter filter = new UnboundElementsSourceFilter();
            handler = new NoUnboundElementsBindHandler(handler, filter);
        }
        if (!validators.isEmpty()) {
            handler = new ValidationBindHandler(handler,
                    validators.toArray(new Validator[0]));
        }
        return handler;
    }
}

Is there any way I can avoid this?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.