杰克逊JSON-使用@JsonTypeInfo批注处理泛型类型

时间:2020-03-12 08:20:46

标签: java json generics jackson jackson-databind

我有要与Jackson和Lombok进行序列化/反序列化的类

@Getter
@NoArgsConstructor
@JsonInclude(NON_NULL)
public class Hostel<T> {

    @JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, property="@class")
    protected @Valid T place;


    public Hostel(T place) {
        this.place = place;

    }
}


@Data
@NoArgsConstructor
@JsonInclude(NON_NULL)
public class MyHostel extends Hostel<Country> {

    @Builder
    public MyHostel(Country country)  {
        super(country);
    }

}

但是在序列化和反序列化时出现此错误

org.springframework.http.converter.HttpMessageNotReadableException: 
JSON parse error: Missing type id when trying to resolve subtype of [simple type, class Country]: missing type id property '@class' (for POJO property 'place'); 
nested exception is com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Missing type id 
when trying to resolve subtype of [simple type, class Country]: missing type id property '@class' (for POJO property 'place')

0 个答案:

没有答案
相关问题