Jackson 2.11.0无法使用@JsonCreator反序列化枚举值

发布时间:2021-03-31 13:53:52阅读:(1346)

最近升级了spring boot的版本,结果发现原本使用@JsonCreator反序列化枚举值都报错了,报错内容如下:

.w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Input mismatch reading Enum `xyz.jieee.demo.DemoEnum`: properties-based `@JsonCreator` ([method xyz.jieee.demo.DemoEnum#getByCode(java.lang.String)]) expects JSON Object (JsonToken.START_OBJECT), got JsonToken.VALUE_STRING; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Input mismatch reading Enum `xyz.jieee.demo.DemoEnum`: properties-based `@JsonCreator` ([method xyz.jieee.demo.DemoEnum#getByCode(java.lang.String)]) expects JSON Object (JsonToken.START_OBJECT), got JsonToken.VALUE_STRING

于是开始排查问题,初步分析是jackson版本升级引起的,于是我开始降版本,当版本降为2.10.5时能正常运行,那么基本可以判断是2.11这个版本中存在Breaking change。

在查看jackson changelog后并没有发现提到相关内容,怀疑可能是jackson的一个bughttps://github.com/FasterXML/jackson/wiki/Jackson-Release-2.11

在jackson相关的issue中搜索了半天,发现好多人都遇到了这个问题,终于在某条评论中找到了解决方案:

@JsonCreator修改为@JsonCreator(mode = JsonCreator.Mode.DELEGATING)

https://github.com/FasterXML/jackson-module-kotlin/issues/336#issuecomment-630587525

标签:jackson

发表评论

评论列表(有0条评论1346人围观)
暂无评论