https://kotlinlang.org logo
#spring
Title
# spring
e

EricJTurley

11/26/2019, 7:50 PM
I tried using
Copy code
@ConstructorBinding
@ConfigurationProperties("push")
data class PushServicesProperties(val snsClient: SnsClient) {

    data class SnsClient(
        val maxConnections: Int,
        val connectionTimeout: Int,
        val socketTimeout: Int,
        val proxyHost: String,
        val proxyPort: Int
    )
}
But I'm still getting
No default constructor found
for the
PushServiceProperties
class. Has anyone else had that problem? Everything looks normal...
e

Egor Babarykin

11/26/2019, 8:18 PM
Did you added this in build.gradle.kts?
Copy code
plugins {
  ...
  kotlin("kapt") version "1.3.60"
}

dependencies {
  ...
  kapt("org.springframework.boot:spring-boot-configuration-processor")
}
e

EricJTurley

11/26/2019, 8:19 PM
Yes - altho in maven.
oh ... waaaaaiiiiit.....
Kotlin 1.3.60 - but not kapt.... Gimme a sec 🙂
Do I need
kapt
to make
@ConstructorBinding
work? Or just to make metadata-generation work?
Well ... I've added it now, but it hasn't made a difference. Tho - for maven I'm not exactly sure what's required.
e

Emerson Farrugia

11/27/2019, 3:30 PM
@EricJTurley I thought it was just for metadata generation
c

Czar

11/28/2019, 6:05 AM
Encountered the same problem today after updating from 2.2.0.RC1 to 2.2.1.RELEASE. Found a workaround: Now we need to annotate embedded property classes with `@ConstructorBinding`as well. In your case:
Copy code
@ConstructorBinding
data class SnsClient(
//...
no other changes are necessary,
kapt
stuff is only needed for metadata generation.
k

kqr

11/28/2019, 1:05 PM
is it workaround? it does make sense, doesn't it?
e

EricJTurley

11/28/2019, 4:53 PM
@Czar I had tried that, too, based on comments I saw in some Spring issues (or something). Didn't work for me. 😞
@kqr It's definitely a workaround. The
@ConstructorBinding
annotation should extend to the nested properties classes.
1
c

Czar

11/28/2019, 4:55 PM
@EricJTurley strange...
e

EricJTurley

11/28/2019, 5:01 PM
Yeah. Just tried again. Specifically, I have a test:
Copy code
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE/*mocked*/,
                classes = EndpointRegistrationServicesConfiguration.class)
class PushServicePropertiesTest {

    @Autowired
    PushServicesProperties pushServicesProperties; // <--- Java properties class

    @Autowired
    PushProperties1 pushProperties1; // <--- Kotlin data properties class
}
And I get:
Copy code
ConfigurationPropertiesBindException
Could not bind properties to 'PushProperties1'
BeanInstantiationException 'PushProperties1'
No default constructor found
NoSuchMethodException: com.blizzard.bnet.push.endpoint.services.PushProperties1.<init>()
OK, I saw a Spring issue that wasn't included until
2.2.1
, so I upgraded. But now I get a different error regarding the nested
SnSClient
class:
Copy code
Parameter 0 of constructor in com.blizzard.bnet.push.endpoint.services.PushServicesProperties1 required a bean of type 'com.blizzard.bnet.push.endpoint.services.PushServicesProperties1$SnsClient' that could not be found.

Failed to load ApplicationContext
No qualifying bean of type 'com.blizzard.bnet.push.endpoint.services.PushServicesProperties1$SnsClient' available
I also added the
@ConstructorBinding
annotation to the inner class
Even tried adding defaults, until the final class is:
Copy code
@ConstructorBinding
@ConfigurationProperties("push1")
data class PushServicesProperties1(val snsClient: SnsClient) {

    @ConstructorBinding
    data class SnsClient(
        val maxConnections: Int = -1,
        val connectionTimeout: Int = 10000,
        val socketTimeout: Int = 10000,
        val proxyHost: String = "",
        val proxyPort: Int? = null
    )
}
12 Views