Я использовал Kotlin при загрузке Spring, и я столкнулся с некоторыми ошибками при создании bean-компонента.
сообщение об ошибке было Index 0 out of bounds for length 0
в классе среды Spring
Я не понимаю, почему возникло исключение из-за этого Java-кода (весна)
for (int paramIndex = 0; paramIndex < paramTypes.length; paramIndex++) {
Class<?> paramType = paramTypes[paramIndex];
String paramName = (paramNames != null ? paramNames[paramIndex] : ""); // Here!
...
}
это возможно?
В любом случае, я хочу знать, как создать Spring Bean как внутренний класс kotlin со значениями свойств (@ConfigurationProperties
).
мой код
// application.yml
naver:
clientId: ...
clientSecret: ...
grantType: authorization_code
redirectUri: ...
accessTokenUrl: ...
profileUrl: ...
// some codes
abstract class OAuth2Provider {
lateinit var clientId: String
lateinit var clientSecret: String
lateinit var grantType: String
lateinit var redirectUri: String
lateinit var accessTokenUri: String
lateinit var profileUri: String
fun callback(code: String) = getProfiles(getAuthenticationResult(code))
abstract fun getAuthenticationResult(code: String): AuthenticationResult
abstract fun getProfiles(result: AuthenticationResult): Map<String, String>
}
@Component
class Providers(
val restTemplate: RestTemplate
) {
private fun createOauth2LoginParams(grantType: String, clientId: String, code: String): MultiValueMap<String, String> {
val map = LinkedMultiValueMap<String, String>()
map.add("grant_type", grantType)
map.add("client_id", clientId)
map.add("code", code)
return map
}
private fun createOauth2HttpEntity(map: MultiValueMap<String, String>): HttpEntity<MultiValueMap<String, String>> {
val headers = HttpHeaders()
headers.contentType = APPLICATION_FORM_URLENCODED
return HttpEntity(map, headers)
}
private fun createAuthenticationResult(url: String, entity: HttpEntity<MultiValueMap<String, String>>): AuthenticationResult {
return restTemplate.postForObject(url, entity, AuthenticationResult::class.java)!!
}
private fun createProfileResponseEntity(url: String, result: AuthenticationResult): ResponseEntity<Map<*, *>> {
val headers = HttpHeaders()
headers.add("Authorization", result.tokenType + " " + result.accessToken)
headers.contentType = MediaType.APPLICATION_FORM_URLENCODED
val entity = HttpEntity<MultiValueMap<String, String>>(headers)
return restTemplate.exchange(url, HttpMethod.GET, entity, Map::class.java)
}
@ConfigurationProperties("naver")
@Component
inner class Naver : OAuth2Provider() {
override fun getAuthenticationResult(code: String): AuthenticationResult {
val map = createOauth2LoginParams(grantType, clientId, code)
map.add("client_secret", clientSecret);
val entity = createOauth2HttpEntity(map)
return createAuthenticationResult(accessTokenUri, entity)
}
override fun getProfiles(result: AuthenticationResult): Map<String, String> {
val resultEntity = createProfileResponseEntity(profileUri, result)
val body = resultEntity.body!!
val uniqueId = body.get("id").toString()
val imageUrl = body.get("profile_image").toString()
val profiles = HashMap<String, String>()
profiles["uniqueId"] = uniqueId
profiles["imageUrl"] = imageUrl
return profiles
}
}
}
ошибка
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.mubeeplayer.api.oauth2.Providers$Naver': Unexpected exception during bean creation; nested exception is java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
...
Caused by: java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:705) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:218) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1341) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1187) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE]
Спасибо. извините за мой английский.