Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@JsonValue annotation in Kotlin's enum class' value property

Tags:

kotlin

jackson

I'm unable to apply the Jackson's @JsonValue annotation on the value parameter of enum class:

enum class CancellationReason(@JsonValue val code: String) {
    CUSTOMER_RESIGNED("20"),
    ERRORS_IN_FOO("21"),
    ERRORS_IN_BAR("24");
}

The error message states: This annotation in not applicable to target 'value parameter'. What's the problem?

like image 542
Tomasz Cudziło Avatar asked Nov 03 '17 15:11

Tomasz Cudziło


Video Answer


1 Answers

You can upgrade jackson-module-kotlin to version 2.9.0, and the error will be gone, because the @JsonValue annotation gets a target FIELD in that version.

Alternatively, fix that by specifying the annotation use-site target by adding @get::

enum class CancellationReason(@get:JsonValue val code: String) {
    CUSTOMER_RESIGNED("20"),
    ERRORS_IN_FOO("21"),
    ERRORS_IN_BAR("24");
}
like image 128
hotkey Avatar answered Oct 11 '22 05:10

hotkey