Spring Boot中Jackson反序列化String转StudentId失败求助
Spring Boot Jackson反序列化String转StudentId失败问题
错误信息
Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.String' to required type 'student.StudentId' for property 'student.ref.null'; Cannot convert value of type 'java.lang.String' to required type 'student.StudentId': no matching editors or conversion strategy found at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:594) at org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyValue(AbstractNestablePropertyAccessor.java:682) ... 104 common frames omitted Caused by: java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'student.StudentId': no matching editors or conversion strategy found at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:262) at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:589) ... 105 common frames omitted
自定义StudentId类
@Data @Getter(onMethod_ = {@JsonValue}) @RequiredArgsConstructor(access = AccessLevel.PRIVATE) public class StudentId { private final long value; @JsonCreator public static StudentId from(long value) { return new StudentId(value); } @JsonCreator public static StudentId from(String value) { long longValue = Long.parseLong(value); return StudentId.from(longValue); } @JsonCreator public static StudentId from(BigDecimal value) { long longValue = value.longValue(); return StudentId.from(longValue); } public String stringValue() { return String.valueOf(value); } }
待转换的JSON片段
"data":{ "Student1":{ "type":"undergrad", "studentId":501043 } }
问题原因分析
错误栈显示是Spring的TypeConverterDelegate抛出的转换失败,而非Jackson直接报错。这说明问题出在Spring的属性绑定机制(比如处理请求参数、配置属性或某些Bean注入场景),而不是纯Jackson的JSON反序列化流程。Jackson的@JsonCreator注解仅在Jackson自身的反序列化过程中生效,无法覆盖Spring独立的类型转换系统。
即便你提供的JSON中studentId是数字类型,但如果实际场景是Spring负责属性绑定(比如表单提交、URL参数传递),Spring会将传入值视为String类型,此时因缺少对应的Spring转换器,就会触发转换失败。
修复方案
方案1:注册Spring类型转换器
创建一个实现Converter<String, StudentId>的类,并将其注册为Spring组件,让Spring在需要转换时自动使用:
import org.springframework.core.convert.converter.Converter; import org.springframework.stereotype.Component; @Component public class StringToStudentIdConverter implements Converter<String, StudentId> { @Override public StudentId convert(String source) { return StudentId.from(source); } }
方案2:针对配置属性场景的特殊处理
如果是绑定@ConfigurationProperties时出现的问题,给转换器添加@ConfigurationPropertiesBinding注解,确保它被用于配置属性的转换:
import org.springframework.boot.context.properties.ConfigurationPropertiesBinding; import org.springframework.core.convert.converter.Converter; import org.springframework.stereotype.Component; @ConfigurationPropertiesBinding @Component public class StringToStudentIdConverter implements Converter<String, StudentId> { @Override public StudentId convert(String source) { return StudentId.from(source); } }
方案3:验证实际请求数据
虽然你提供的JSON中studentId是数字,但实际请求中可能存在studentId被传为字符串的情况(比如前端表单提交、URL参数)。可以通过抓包工具确认实际请求的参数类型,确保传入值符合预期。
内容的提问来源于stack exchange,提问作者Kalhara
相关产品推荐
相关产品推荐

