Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

256
Visualizações
Spring no setter found for property

I'm trying to make a configuration data class in Kotlin with Spring Boot. I have the following class:

@Configuration
@ConfigurationProperties(prefix = "spring.braintree")
data class BraintreeConfigProperties(
    val merchantId: String = "",
    val publicKey: String = "",
    val privateKey: String = "",
    val environment: Environment = Environment.SANDBOX
)

And the following properties:

spring.braintree.merchantId=${BRAINTREE_MERCHANT_ID}
spring.braintree.publicKey=${BRAINTREE_PUBLIC_KEY}
spring.braintree.privateKey=${BRAINTREE_PRIVATE_KEY}

When I run the application I get the following exception:

APPLICATION FAILED TO START
***************************

Description:

Failed to bind properties under 'spring.braintree' to com.x.ps.config.BraintreeConfigProperties$$EnhancerBySpringCGLIB$$311e0531:

    Property: spring.braintree.merchant-id
    Value: ${BRAINTREE_MERCHANT_ID}
    Origin: class path resource [application.properties] - 5:29
    Reason: java.lang.IllegalStateException: No setter found for property: merchant-id

Action:

Update your application's configuration

However, I also have a class which is exactly the same as the BraintreeConfigProperties class. This class (DatabaseConfigProperties) does work. They are completely identical.

What I've tried:

  1. Checked if the property names actually match with the configuration names.
  2. Tried all three configuration properties separately.
  3. Attached a non environment variable with value 1234 to the merchantId property which results in the following exception:
APPLICATION FAILED TO START
***************************

Description:

Failed to bind properties under 'spring.braintree' to com.x.ps.config.BraintreeConfigProperties$$EnhancerBySpringCGLIB$$62509e61:

    Property: spring.braintree.merchant-id
    Value: 1234
    Origin: class path resource [application.properties] - 5:29
    Reason: java.lang.IllegalStateException: No setter found for property: merchant-id

Action:

Update your application's configuration
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

The proplem here ist that val is final and var can be changed. So you should change your properties to var.

More Info here: https://medium.com/mindorks/kotlin-series-val-vs-var-difference-android-kotlin-ecad780daeb7

over 4 years ago · Santiago Trujillo Relatório

0

Kotlin only generates a getter, when declaring a field as val. Spring however uses a setter to inject values from properties into the object.

With newer versions of Spring Boot you can make use of data classes with val fields, by injecting values through the constructor instead of using setters.

You can do so by using @ConstructorBinding, see the following example from the official docs:

@ConstructorBinding
@ConfigurationProperties("example.kotlin")
data class KotlinExampleProperties(
        val name: String,
        val description: String,
        val myService: MyService) {

    data class MyService(
            val apiToken: String,
            val uri: URI
    )
}

While using lateinit var or a var that's initialized to null works too. Most likely you don't want to change those fields after initialization, thus val seems to fit better most of the time instead of declaring a var and generating a setter under the hood.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda