```class SeonApi { companion object { ...
# announcements
d
Copy code
class 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?
v
If it fails to compile, what is the compile error?
Probably unreachable code as you start your function with a
return
and everything after that can never be reached?
d
I commented in the code
It said "annotation class cannot be instantiated
v
Yeah, isn't that obvious?
@Value("...")
tries to create an instance of the annotation class
Value
and that cannot work
e
Annotations bind to the proceeding element (see
java.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 ­čÖé
d
Tried to understand what you were saying, with some Google around your suggestion and updated the code as follow.
Copy code
companion 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 function
Sorry for kinda naive question, I dont have experience with Spring prior