I am using @ConfigurationProperties to load properties. I have added @Validated to enable validations. Currently port is configured as wwww, I get the error on Spring Boot startup but MyValidationBindHandler that I have setup is not getting invoked. Is there any configuration issue that I am missing? Unfortunately there is limited documentation on usage of ValidationBindHandler. Please let me know if there is any configuration that I am missing.

ConfigProperties.java

package com.example.demo;

import javax.validation.constraints.NotNull;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

@ConfigurationProperties(prefix = "mail", ignoreInvalidFields=false)
@Component
@Validated
public class ConfigProperties {

@NotNull private String hostName;
    private int port;
    private String from;
    public String getHostName() {
        return hostName;
    }
    public void setHostName(String hostName) {
        this.hostName = hostName;
    }
    public int getPort() {
        return port;
    }
    public void setPort(int port) {
        this.port = port;
    }
    public String getFrom() {
        return from;
    }
    public void setFrom(String from) {
        this.from = from;
    }
}

MyValidationBindHandler.java

package com.example.demo;

import org.springframework.boot.context.properties.bind.BindContext;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.validation.ValidationBindHandler;
import org.springframework.boot.context.properties.source.ConfigurationPropertyName;
import org.springframework.stereotype.Component;

@Component
public class MyValidationBindHandler extends ValidationBindHandler{

    @Override
    public Object onFailure(ConfigurationPropertyName name, Bindable<?> target, BindContext context, Exception error)
            throws Exception {
        System.out.println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX "+name);
        return super.onFailure(name, target, context, error);
    }
}

ConfigPropertiesDemo.java

package com.example.demo;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ConfigPropertiesDemo {

    @Autowired
    ConfigProperties configProperties;

    @PostConstruct
    public void printValues() {
        System.out.println("getFrom :: "+configProperties.getFrom());
        System.out.println("getHostName :: "+configProperties.getHostName());
        System.out.println("getPort :: "+configProperties.getPort());
    }
}

application.yml

mail.port: wwww 

Error

Failed to bind properties under 'mail.port' to int:

    Property: mail.port
    Value: wwww
    Origin: class path resource [bootstrap.yml]:24:12
    Reason: failed to convert java.lang.String to int

Action:

Comment From: gituserjava

  • @mbhave , @philwebb

Comment From: philwebb

@gituserjava No need to @ mention us, we're already subscribed to this tracker.

Comment From: wilkinsona

ValidationBindHandler is used internally by ConfigurationPropertiesBinder. There's no need for you to configure it, or a sub-class of it, yourself. As described in the documentation, you can provide a custom Validator by defining a static @Bean named configurationPropertiesValidator.

If you have any further questions, please follow up on Stack Overflow or Gitter. As mentioned in the guidelines for contributing, we prefer to use GitHub issues only for bugs and enhancements.