dimanche 11 juillet 2021

Unable to find @JsonProeprty annotation with field.getAnnotation

I'm using kotlin 1.4.31.
My class is like below

class SomeClass(
  @JsonProperty("trans_name")
  val transactionName,
  @JsonProperty("trans_dtime")
  val transactionDateTime
  // ...
)

I used reflection to get @JsonProperty's value because it's needed in our code.

SomeClass::class.java.declaredFields
  .map { it.declaredAnnotationByType(JsonProperty::class.java) }
  .map { it.first().value }

But exception was occurred

java.util.concurrent.CompletionException: java.util.NoSuchElementException: Array is empty

Exception reason is reflection couldn't find @JsonProperty annotation.
Even when I called it.declaredAnnotations() directly, the size was zero.

Oddly enough, annotations like @Column work well.

What is different between @Column and @JsonProperty, and how can I access @JsonProperty using reflection?





Aucun commentaire:

Enregistrer un commentaire