小编For*_*gel的帖子

JacksonPolymorphicDeserialization:JsonMappingException

我有一个父类参数,它有2个子类ComboParameterIntegerParameter

@JsonSubTypes({
    @JsonSubTypes.Type(value = IntegerParameter.class, name = "integerParam"),
    @JsonSubTypes.Type(value = ComboParameter.class, name = "comboParam")
})
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = As.WRAPPER_OBJECT)
 public abstract class Parameter {
String regEx;
}


@JsonTypeName("integerParam")
public class IntegerParameter extends Parameter {
}

@JsonTypeName("comboParam")
public class ComboParameter extends Parameter {
List<String> values;
}
Run Code Online (Sandbox Code Playgroud)

我有一个具有属性参数的类

class A {
@JsonUnwrapped
Parameter parameter;
}
Run Code Online (Sandbox Code Playgroud)

对象的序列化A抛出异常

com.fasterxml.jackson.databind.JsonMappingException:未包装的属性需要使用类型信息:无法在不禁用的情况下序列化 SerializationFeature.FAIL_ON_UNWRAPPED_TYPE_IDENTIFIERS

如果我删除注释,@JsonUnwrapped我会有一个像那样的json

{
     parameter:{
          integerParam:{
               regEx: regExVal
          }
     }
}
Run Code Online (Sandbox Code Playgroud)

而我需要的是像这样的json:

{ …
Run Code Online (Sandbox Code Playgroud)

serialization json spring-mvc jackson deserialization

9
推荐指数
1
解决办法
976
查看次数