SpringBoot--解决空字符串转枚举异常

原文网址:SpringBoot--解决空字符串转枚举异常_IT利刃出鞘的博客-CSDN博客

简介

本文介绍如何解决Java的SpringBoot中空字符串转枚举时报错的问题。

问题复现

org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type *.**.**.PayType from String "": value not one of declared Enum instance names: [ALIPAY, WECHAT_PAY, BANK_UNION]; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type *.**.**.PayType from String “”: value not one of declared Enum instance names: [ALIPAY, WECHAT_PAY, BANK_UNION]

原因分析

字符串与枚举类的对应关系是:字符串对应枚举类的字面量(或者说name()方法);null对应null,如果是空字符串,会找不到对应的枚举类,就会报错。

解决方案

上边是文章的部分内容,为便于维护,全文已转移到此网址:SpringBoot-解决空字符串转枚举异常 - 自学精灵

猜你喜欢

转载自blog.csdn.net/feiying0canglang/article/details/128321067