Kotlin的enum类的value属性中的@JsonValue注释

我无法在枚举类的值参数上应用Jackson的@JsonValue注释:

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

错误消息指出: This annotation in not applicable to target 'value parameter' 。 有什么问题?

您可以将jackson-module-kotlin升级到版本2.9.0,并且错误将会消失,因为@JsonValue批注在该版本中获取目标FIELD

或者,通过添加@get:来指定注释use-site目标来解决这个问题。

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