79446947

Date: 2025-02-18 01:54:12
Score: 0.5
Natty:
Report link

When adding an extra constructor to a @ConfigurationProperties annotated record in Spring Boot, you need to explicitly tell Spring Boot which constructor to use for binding by annotating it with @ConstructorBinding.

@ConfigurationProperties("user")
record User(String name, int age) {

    @ConstructorBinding
    public User { // Annotate the canonical constructor
    }

    public User(String name) {
        this(name, 0);
    }
}

@ConstructorBinding is required when multiple constructors exist to avoid ambiguity.

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Starts with a question (0.5): When addin
  • Low reputation (1):
Posted by: Bel homme