0

i'm adding property validation to an existing big project. It has hundrets of webservices and there are some that have simple numbers as names. Now im trying to write a data class using @Validated, @ConstructorBinding and @ConfigurationProperties. So imagine a property dummy.941=http:... The name of the variable would need to be 941 now, as far as i can tell, but kotlin/java dont allow variable names starting with numbers.

@Validated
@ConstructorBinding
@ConfigurationProperties(value = "dummy", ignoreUnknownFields = false)
data class DummyProperties(

    val abc: Abc = Abc(), ....

    val 941: Ws941: Ws941()
)

Is there any workaround, some annotation, that says which property is meant? It is not possible to change the name of the property, since the same property database is in use different working systems and people told me thats off the table.

Thanks for any help!

EDIT: I found a way, spring offers a @Name annotation (org.springframework.boot.context.properties.bind)

 @Valid
 @Name(value = "703")
 val s703: S703 = S703(),

Works like a charm:)

1 Answers1

0

Some time ago, I had a similar issue. You can solve it, at least for Java, by using a custom setter. I have no idea about Kotlin, but I assume it works in the same way for Spring Kotlin.

@ConfigurationProperties(value = "dummy", ignoreUnknownFields = false)
public class DummyProperties {

    private Ws941 _941;

    public void set941(Ws941 _941) {
        this._941 = _941;
    }

    public Ws941 get941() {
        return this._941;
    }
}

Spring can map using the setter, so the variable can have a different name.

0x1C1B
  • 1,204
  • 11
  • 40