Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot not failing if property is missing

How can I make Spring-Boot fail during startup if any of the properties of a @ConfigurationProperties class is missing?

If I use @Value(...) instead, the auto-wiring fails as expected, but I want to centralise all properties into a @ConfigurationProperties class instead of using @Value references spread through the application.

The code:

@Component
@ConfigurationProperties(value = "serving.api", ignoreUnknownFields = false)
public class ApplicationProperties {

    private String projectId;

    private String bigTableInstanceId;

    public String getProjectId() {
        return projectId;
    }

    public void setProjectId(String projectId) {
        this.projectId = projectId;
    }

    public String getBigTableInstanceId() {
        return bigTableInstanceId;
    }

    public void setBigTableInstanceId(String bigTableInstanceId) {
        this.bigTableInstanceId = bigTableInstanceId;
    }

}
like image 915
cahen Avatar asked Mar 02 '17 16:03

cahen


1 Answers

You can achieve this by adding a @NotNull validator to your mandatory properties. For example:

@NotNull
private String projectId;

public void setProjectId(String projectId) {
    this.projectId = projectId;
}
like image 53
Andy Brown Avatar answered Oct 10 '22 20:10

Andy Brown