Dany
05/04/2021, 9:40 PMclass SeonApi {
companion object {
fun getEmailScore(emailInput: String): String {
return "hello"
var request: HttpRequest = HttpRequest.newBuilder()
.GET()
.uri(URI.create("<https://api.seon.io/SeonRestService/email-api/v2.0/>".plus(emailInput)))
.setHeader("X-API-KEY", @Value("\${seon_api_key}")) //***************complained: "Annotation class cannot be instantiated"
.build()
var response: HttpResponse<String> = HttpClient.newHttpClient().send(request, BodyHandlers.ofString())
return response.body()
}
}
}
This piece of code failed to compile and the debug. Had been spending 30 minutes debuging + googling without any success, anyone has a suggestion?Vampire
05/04/2021, 9:51 PMVampire
05/04/2021, 9:52 PMreturn
and everything after that can never be reached?Dany
05/04/2021, 9:52 PMDany
05/04/2021, 9:52 PMVampire
05/04/2021, 9:54 PM@Value("...")
tries to create an instance of the annotation class Value
and that cannot workEmil Kantis
05/04/2021, 10:02 PMjava.lang.annotation.ElementType
for valid targets). @Value
for instance is commonly used to annotate constructor arguments or fields of a class to enable Spring injection. In your case you have no proceeding element, so it's just invalid code.
Another, more vanilla way, of achieving what I believe you want is to put your API key as an environment variable and load it with System.getenv(variableName)
. Or you could go read up more on how to use Spring 🙂Dany
05/04/2021, 10:17 PMcompanion object {
fun getEmailScore(emailInput: String): String {
@Value("\${seon_api_key}")
val seon_api_key: String = ""
var request: HttpRequest = HttpRequest.newBuilder()
.GET()
.uri(URI.create("<https://api.seon.io/SeonRestService/email-api/v2.0/>".plus(emailInput)))
.setHeader("X-API-KEY", this.seon_api_key)
.build()
var response: HttpResponse<String> = HttpClient.newHttpClient().send(request, BodyHandlers.ofString())
return response.body()
}
}
Now it complains "This annotation is not applicable to target 'local variable'". And I was looking at example on Google, it was callabe inside a functionDany
05/04/2021, 10:17 PM