Redis增加字段,獲取數(shù)據(jù)反序列化失敗

背景

1.有2個項目a,b,使用了同一個redis服務(wù)中的對象數(shù)據(jù),key-value

"test":{"a":null,"b":null}

2.項目a,新增了一個字段c,并編輯了redis中的數(shù)據(jù)

"test":{"a":null,"b":null,"c":null}

3.項目a可以正常運行,但是項目b獲取test的val時,反序列化失敗

org.springframework.data.redis.serializer.SerializationException: 
Could not read JSON: 
Unrecognized field "c" (class com.ty.test),
not marked as ignorable (2 known properties: "a", "b"])

原因

反序列化的時候沒有找到對應(yīng)的set方法

 /**
     * Helper method called for an unknown property, when using "vanilla"
     * processing.
     *
     * @param beanOrBuilder Either POJO instance (if constructed), or builder
     *   (in case of builder-based approach), that has property we haven't been
     *   able to handle yet.
     */
    protected void handleUnknownVanilla(JsonParser p, DeserializationContext ctxt,
            Object beanOrBuilder, String propName)
        throws IOException
    {
        if (_ignorableProps != null && _ignorableProps.contains(propName)) {
            handleIgnoredProperty(p, ctxt, beanOrBuilder, propName);
        } else if (_anySetter != null) {
            try {
               // should we consider return type of any setter?
                _anySetter.deserializeAndSet(p, ctxt, beanOrBuilder, propName);
            } catch (Exception e) {
                wrapAndThrow(e, beanOrBuilder, propName, ctxt);
            }
        } else {
            // Unknown: let's call handler method
            handleUnknownProperty(p, ctxt, beanOrBuilder, propName);         
        }
    }

    /**
     * Method called when a JSON property is encountered that has not matching
     * setter, any-setter or field, and thus cannot be assigned.
     */
    @Override
    protected void handleUnknownProperty(JsonParser p, DeserializationContext ctxt,
            Object beanOrClass, String propName)
        throws IOException
    {
        if (_ignoreAllUnknown) {
            p.skipChildren();
            return;
        }
        if (_ignorableProps != null && _ignorableProps.contains(propName)) {
            handleIgnoredProperty(p, ctxt, beanOrClass, propName);
        }
        // Otherwise use default handling (call handler(s); if not
        // handled, throw exception or skip depending on settings)
        super.handleUnknownProperty(p, ctxt, beanOrClass, propName);
    }

解決方法

在每個redis的實體類上新增注解

//添加注解,忽略不存在的key
@JsonIgnoreProperties(ignoreUnknown = true)
public class Test
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容